How to convert PNG from alpha to JPEG, preserving colors using JAVA

I'm having trouble converting PNG from Alpha to JPEG from the Wiki.

This image is: http://upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Radio_SRF_3.svg/500px-Radio_SRF_3.svg.png

Original: enter image description here

The converted JPEG file has the wrong color. Now it is more gray than darker. image result

This is how I do the conversion:

Delete alpha:

public static BufferedImage imageFillAlphaWithColor(BufferedImage image, Color fillColor) { if (image.getColorModel().getTransparency() == Transparency.OPAQUE) return image; int w = image.getWidth(); int h = image.getHeight(); BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, fillColor, null); g.dispose(); return newImage; } 

Jpeg compression:

 public static byte[] compressedJpegImage(BufferedImage image, float quality) { byte jpegImage[] = null; try { // Find a jpeg writer ImageWriter writer = null; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = (ImageWriter) iter.next(); } // Prepare output ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios); // Set the compression quality ImageWriteParam iwparam = writer.getDefaultWriteParam(); iwparam.setProgressiveMode(ImageWriteParam.MODE_DEFAULT); iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwparam.setCompressionQuality(quality); // Write the image writer.write(null, new IIOImage(image, null, null), iwparam); // Cleanup ios.flush(); writer.dispose(); ios.close(); jpegImage = os.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return jpegImage; } 

And save it as JPEG using ImageIO.

Any idea how to preserve the color space in order to preserve the colors as they are in the original?

UPDATE

The problem was caused by ImageMagick when resizing. ImageMagick seems to have changed the color space to TYPE_CUSTOM, and Java can't handle it.

Current solution:

Remove alpha before I start resizing the image using ImageMagick.

No solution has yet been found to convert the color space from "CUSTOM" back to "ARGB". So this is just a "workaround" for this problem.

+7
java colors png alpha jpeg
source share
3 answers

In fact, you are not deleting alpha values. Java divides color values ​​into alpha when you do what you did in the imageFillAlphaWithColor method.

I recommend using the AlphaComposite class to actually remove alpha.

Here's an Oracle tutorial on how Composite Graphics: http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html

Unable to easily test, I think you need to do the following:

 Graphics2D g = newImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(image, 0, 0, fillColor, null); 

If this does not work, read this related question: Change the alpha value of BufferedImage?

+3
source share

How about this?

 public static void main(String[] args) throws Exception { Color fillColor = Color.BLUE; // just to verify BufferedImage bi = ImageIO.read(new File("c:\\temp\\tGjhw.png")); BufferedImage bi2 = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_3BYTE_BGR); bi2.getGraphics().drawImage(bi, 0, 0, fillColor, null); // you can do a more complex saving to tune the compression parameters ImageIO.write(bi2,"JPG",new File("c:\\temp\\tGjhw2.jpg")); } 
0
source share

A too bright result is apparently related to the conversion from a linear color space (gamma = 1) to sRGB (gamma = 1 / 2.2). The original does not contain gamma information, so ImageMagick is free to choose its own default. Recent versions of ImageMagick process grayscale images in different ways, some suggesting linear and others suggesting sRGB. From the command line, you should use the "-gamma 1" option to get a darker result with all versions. You need to make an equivalent in Java.

0
source share

All Articles