Skip to main content

Convert image to indexed color

2 replies [Last post]
Anonymous

I'm using Java2D (with ImageIO) to produce a PNG file. This works OK.

But how do I use Java2D to convert the image to an indexed color
format (16 or 256 colors) before writing the PNG file?

Are there are different ways of reducing the number of colors?

Cheers,

Ernst

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris Campbell

Hi Ernst,

Probably the most straightforward way would be to render your
original image into an 8-bit indexed image and then send that to
ImageIO instead:

BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED);
Graphics g = img.createGraphics();
g.drawImage(orig, 0, 0, null);
g.dispose();
ImageIO.write(img, "png", file);

The drawImage() call will take care of downsampling your original 24-
bit image into an 8-bit IndexColorModel automatically. If you want
to have more control over the color map (or you want to create a 16-
color PNG), you can provide your own IndexColorModel when
constructing a BufferedImage, but the above code is usually
sufficient in most cases.

Chris

On Jul 26, 2007, at 3:29 AM, Ernst de Haan wrote:
> I'm using Java2D (with ImageIO) to produce a PNG file. This works OK.
>
> But how do I use Java2D to convert the image to an indexed color
> format (16 or 256 colors) before writing the PNG file?
>
> Are there are different ways of reducing the number of colors?
>
> Cheers,
>
>
> Ernst
>
> ======================================================================
> =====
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Ernst de Haan

Chris,

Thanks a lot! Much appreciated! I will give it a try.

Ernst

Op 26-jul-2007, om 19:13 heeft Chris Campbell het volgende geschreven:

> Hi Ernst,
>
> Probably the most straightforward way would be to render your
> original image into an 8-bit indexed image and then send that to
> ImageIO instead:
>
> BufferedImage img = new BufferedImage(w, h,
> BufferedImage.TYPE_BYTE_INDEXED);
> Graphics g = img.createGraphics();
> g.drawImage(orig, 0, 0, null);
> g.dispose();
> ImageIO.write(img, "png", file);
>
> The drawImage() call will take care of downsampling your original
> 24-bit image into an 8-bit IndexColorModel automatically. If you
> want to have more control over the color map (or you want to create
> a 16-color PNG), you can provide your own IndexColorModel when
> constructing a BufferedImage, but the above code is usually
> sufficient in most cases.
>
> Chris
>
> On Jul 26, 2007, at 3:29 AM, Ernst de Haan wrote:
>> I'm using Java2D (with ImageIO) to produce a PNG file. This works OK.
>>
>> But how do I use Java2D to convert the image to an indexed color
>> format (16 or 256 colors) before writing the PNG file?
>>
>> Are there are different ways of reducing the number of colors?
>>
>> Cheers,
>>
>>
>> Ernst
>>
>> =====================================================================
>> ======
>> To unsubscribe, send email to listserv@java.sun.com and include in
>> the body
>> of the message "signoff JAVA2D-INTEREST". For general help, send
>> email to
>> listserv@java.sun.com and include in the body of the message "help".
>

Groet,

Ernst de Haan
PensioenPage B.V.

Postbus 263, 6880 AG Velp
(026) 364 56 34

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".