coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 784 forks source link

I want to turn a picture of PNG into a JPG #130

Closed smokingrain closed 6 years ago

smokingrain commented 6 years ago

Expected behavior

PNG -> JPG width = height = 300 -> width = height = 300 size=22kb -> 10kb or smaller

Actual behavior

PNG -> JPG width = height = 300 -> width = height = 300 size=22kb -> 50kb God ,maybe I'm blind,shouldn't it be smaller?

Steps to reproduce the behavior

Here is my code: Thumbnails.of(source).size(widthDest, heightDest).outputFormat("JPEG").outputQuality(1.0f).toFile(dest);

Environment

bgunnink commented 6 years ago

No; or at least not at 100% quality. Get Paint.NET, open your PNG, and start saving as a JPEG. When you crank the quality slider to 100%, it's quite a bit larger than the original. The pivot point tends to be 95%, in my experience.

coobird commented 6 years ago

Yes, as @bgunnink mentions, it is not unexpected to see JPEGs ending up larger than PNGs in certain circumstances.

It really comes down to how those two image compression schemes work -- to put simply, JPEG is designed to compress natural images like photographs, while PNG is designed to compress images that have stark contrasts and large sections of images with the same color, like illustrations. Also, JPEG is lossy compression format, so when you try to compress images that it isn't suited for (like illustrations), perceptually, you may need to crank up the image quality slider to 100%, so you end up with file sizes that would be larger than compressing with PNG.

This isn't really an issue with Thumbnailator itself, but with properties of different image compression formats.