Search code examples
javaimage-processingjpegthumbnailsjavax.imageio

Image size increased twice when convert from JPG to PNG using thumbnailator


Am using Thumbnailator to compress the image in my application. Everything is work fine alone when i try to convert the JPG image to PNG. At this process the size of an image getting twice after compressing. Following code is am used to convert image.

File a=new File("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb.jpg");
Thumbnails.of(a).scale(1).outputQuality(0.5).toFile("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb1.png");

using pure java also doing same and code is follows

BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb.jpg"));
ImageIO.write(bufferedImage, "png", new File("C:\\Users\\muthu\\Downloads\\javaPngimage.png"));

Ex: 5MB image file is converted to 32MB file. I should not go for resize to compress. Am stuck with this


Solution

  • JPEG and PNG are both compressed image formats.

    JPEG compresses the pixels using frequency transforms and quantisation. It can be a lossy or lossless compression format.

    PNG is a lossless compression format with different compression mechanisms. I dare say the "quality" parameter doesn’t actually change the image at all.

    The biggest image file type would be BMP (.bmp), which is 3 bytes (RGB) for each pixel plus a header. It’s worth keeping this size in mind when deciding if an image file is "big" or not. JPEG compression is pretty good.

    It sounds like your image has a lot of details that can be compressed well in the frequency domain (JPEG) but compress poorly as PNG.

    Simplest solution: a JPEG format thumbnail. If you needed to use PNG, and you were resizing your image, I’d suggest resize JPEG then convert to PNG.