I am using ImageIO.write() to convert PNG files to JPG. For some reason, my result image has a pink layer over it. I have searched far and wide for a solution but haven't found any. The code works for all other types of images except PNG.
4 Answers
I'm not sure how the other code snippet works given buffer is not used after it's created. I've found this pink problem to be jvm version specific.
The easiest solution I've found is to do this.
BufferedImage image = null;
BufferedImage imageRGB = null;
// imageBytes is some png file you read
image = ImageIO.read(new ByteArrayInputStream(imageBytes));
// Attempt at PNG read fix
imageRGB = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_INT_RGB);
// write data into an RGB buffered image, no transparency
imageRGB.setData(image.getData());
// return the RGB buffered image, or do ImageIO.write( ... );
return imageRGB; // fixed for jpeg

- 63
- 1
- 5
-
8This solution does not work in my case - it creates a gray scaled image with vertical lines. – Robert May 21 '13 at 14:27
-
1Didn't work to save image as .jpg in JavaFX. – Zon Oct 25 '13 at 18:21
-
4I had bad luck with the setData() part - the image developed lines like @Robert mentioned. It looked like 4 bytes of data was being squished into a 3 byte hole, which is what happens when you go from ARGB to RBG. Anyway, `imageRGB.createGraphics().drawImage(image, null, null);` works instead. – KevinL Aug 06 '14 at 13:06
-
I get the same as Robert, grayscale image with vertical lines. Sorry. – cthulhu Dec 15 '14 at 10:44
-
However, @KevinL's change does work - the non-transparent part of the image gets converted properly. The transparency is turned to black though. Will have to fiddle with that. – cthulhu Dec 15 '14 at 10:53
-
Not work. Create gray image – Víctor Dueñas Robles Dec 23 '15 at 08:51
-
upvote for the complete code snippet, however it must be used on conjunction with @KevinL 's suggested fix. So +1 for him too. Go team. – lolynns Aug 14 '20 at 18:25
Quick reading of other SO answers tagged ImageIO led to this.
The root cause can be a buggy reader. The proposed workaround is using different reader package.
Edit Above link is broken, but this appears to be it.
Edit The above links are broken, here it is on archive.org.

- 37
- 11
-
Do you have any suggestions for other reader packages? I am using javax.imageio.ImageIO. Don't know of any others out there. – Adam Stokar Dec 02 '09 at 01:44
-
I too had the same problem, but if i write it in png format then it gets solved.
Something like this,
ImageIO.write(resizedImageBuffer, "png", baos);

- 4,038
- 9
- 36
- 61

- 187
- 3
- 10
I found this link which has some code that might be of use. I tried your code with a few of my images but I couldn't reproduce the issue. I tried the last answer by devyn_a and it didn't break anything. Here's your code modified with devyn_a's solution.
String url = "file:///d:/teststuff/IMG_0393.JPG";
String to = "d:/teststuff/out.jpg";
BufferedImage oldImage = ImageIO.read(new URL(url));
BufferedImage buffer = new BufferedImage (oldImage.getWidth(),
oldImage.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageIO.write(ImageIO.read(new URL(url)), "jpg", new File(to));
It would be interesting to know if this resolves the issue.

- 1,065
- 8
- 8
-
2Hm, I don't understand this answer at all. The oldImage and buffer are not used after creation, so why should they help? – Christian Gawron Mar 22 '13 at 21:55
-