Convert PNG to JPEG image file in Java

Please expect some quality loss when convert image file from PNG to JPEG, because both are different format, some effects in PNG may not exist in JPEG image file. For example, transparent background in PNG is not support in JPEG image file, Google it for more information.

Example

Here’s a Java example to convert a PNG file with transparent background, to a JPEG image with white background.

File : javanullpointer.png, 4kb

png file with transparent background

Run below code.


package com.mkyong;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ConvertImageFile {

   public static void main(String[] args) {

	BufferedImage bufferedImage;
		
	try {
			
	  //read image file
	  bufferedImage = ImageIO.read(new File("c:\\javanullpointer.png"));

	  // create a blank, RGB, same width and height, and a white background
	  BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
			bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
	  newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

	  // write to jpeg file
	  ImageIO.write(newBufferedImage, "jpg", new File("c:\\javanullpointer.jpg"));

	  System.out.println("Done");
			
	} catch (IOException e) {

	  e.printStackTrace();

	}

   }

}

Output

A new jpg file is generated, with white background.

File : javanullpointer.jpg, 9kb

jpg file with white background
Note
This code is tested with few PNG image files, the converted JPEG image file is in acceptable quality.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
Sprague PearlanonymousSatheeshraj VAlex ArriagaChandra Mani Gupta Recent comment authors
newest oldest most voted
Alex Arriaga
Guest
Alex Arriaga

Thanks, it works like a charm!

Sprague Pearl
Guest
Sprague Pearl

Can somebody please help me! I need to convert jpg to pdf document. I only know of this adress and would like some comments on how does it work. Thanks!

Satheeshraj V
Guest
Satheeshraj V

Hi,
I need the code to convert .gif image with transparent background to resized .jpg image ..I tried with different possibilities in the internet but no luck .I’m always getting the black image .please help me to resolve it.

Regards,
Satheesh

anonymous
Guest
anonymous

Just make sure you are not using
this : BufferedImage.TYPE_INT_ARGB

Chandra Mani Gupta
Guest
Chandra Mani Gupta

Thanks dude.

David
Guest
David

Hey thanks for sharing the source. Does this only work for small image conversions? What if you have a 2448×3264 PNG image (~13 MB) for converting to JPEG? Trying th code, it threw exception with Java out of memory error with heap space.