-1

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

Muthu vignesh k
  • 237
  • 1
  • 4
  • 19
  • 1
    There's a difference between "resizing" and "compressing" - yes, resizing will change the size which will change the byte size, but compressing applies an algorithm to the existing byte data in order to reduce it's over size (which then needs to be uncompressed later), also, there is a difference between jpg and png compression. Jpg depends to be better, but reduces the quality of the image - in general. Although I'm not quite sure why you might get such a discrepancy – MadProgrammer Feb 20 '18 at 19:50

1 Answers1

2

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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
Pam
  • 1,146
  • 1
  • 14
  • 18
  • Not sure I follow why resizing the JPEG and then converting the PNG would help? Most likely, you would introduce additional JPEG artifacts that would compress worse using PNG I think. I think your answer covers the most important details though. Ie. this size increase is to be expected. +1 – Harald K Feb 21 '18 at 09:23
  • Well, if you resize and then compress, the lossy JPEG will remove some image details and the PNG *might* end up smaller (but, yes, it also might end up bigger!). – Pam Feb 21 '18 at 10:23