Suppose you have a .jpg photo such as this apple:
(.jpg 24KB)
You use GIMP to add an alpha channel and remove the white background. As .jpg does not support transparency (see this question), you have to export as a .png file (90KB).
The photo is meant to be used on a webpage or in a (LaTeX) document, and you do not which to have the increased file size. You open the .png file in GIMP and export it as .png, while selecting a compression level of 9 (the maximum). However, this yields a .png file of 96KB.
-> Why? Is there a way to obtain a .png file arount the original file size of 24KB?
-
Compression level isn't really a compression amount. It just sets how hard the algorithm tries. For some image such as your apple is is mostly a waste of CPU because there will be little to compress outside of the obvious transparent background so even complex strategies won't find anything.– xenoidCommented Oct 2, 2023 at 20:21
-
But why is the first .png (after removing the white background) bigger than the original .jpg?– KarloCommented Oct 3, 2023 at 13:12
-
1In addition to the lossless/lossy difference, bear in mind that an alpha channel increases size. So if one channel is X, instead of X times 3, it is X times 4. PNG has multiple ways to handle alpha though.– YorikCommented Oct 3, 2023 at 15:09
-
@Karlo Because a JPEG compression is "lossy". The image you get after decompression is a "simplified" version of the image you compress. With PNG the output image is strictly identical.– xenoidCommented Oct 3, 2023 at 16:02
2 Answers
I don't think what you want is possible with an RGBa PNG image. This has little to do with GIMP itself. The problem is that it's a lossless format, so it's always going to be bigger than a corresponding RGB JPEG which is a lossy format that allows you to add compression to keep the file size down.
You might want to consider trying a different format such as WebP. GIMP supports export in that format. It allows you to use lossy compression like a JPEG, but retain the alpha transparency. Essentially WebP is almost like an RGBa JPEG, which is not normally possible.
An example. Here's a WebP image that is around 24kb. I saved this at 100% quality, so compression is minimal. The only downside is that not everything supports WebP - some image sharing sites such as imgur don't. Some older browsers might not support it either.
Here are my WebP export settings
And here is the file size
-
1Note: There are also online PNG compression converters, but they will reduce the bit depth from 32 to 8, essentially creating an indexed colour image, which will impact image quality, and even then I think it would still be above 24kb Commented Oct 2, 2023 at 14:20
-
Interesting. Too bad that one cannot use WebP images in LaTeX.– KarloCommented Oct 3, 2023 at 13:17
Broadly, JPEG compression is designed to be good at compressing photographs. PNG compression, besides being non-lossy, is designed to be good at compressing rendered vector graphics; it has algorithms to efficiently compress sharp-edged regions of solid color, gradients, and similar things.
Therefore, you can make an image more PNG-compressible by making it more like vector art. I took your apple and used GIMP's Posterize function (20 levels) to produce this image, which is 28.3 KB after compression:
Another option is dithering, which also uses fewer colors but mixes them in patterns, so there is less visible banding but more speckledness. Dithering is also handled well by PNG as long as the dither patterns are consistent; I chose the “Bayer” dither pattern set in GIMP for this image, 20.6 KB:
In both cases, you can choose the number of colors to reduce to and get a larger or smaller image.
I was able to reduce the size even further using the pngcrush
tool, to 14.6 KB. This image is visually identical to the previous, but even smaller, because pngcrush -brute
will try harder than GIMP does (at the cost of spending several seconds finding the best compression strategy).