Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My .jpg file is larger than .png?

I used Microsoft Paint to create a 15248 x 6552 solid color picture. I saved it as both .png and .jpg and was expecting the .jpg to be smaller than .png, but it was not. The .jpg file is 1.49MB, while the .png is 391KB. Shouldnt jpeg being a lossy compression be technically smaller in size?

I read somewhere that .png is better for solid colors etc, so I downloaded a picture form the web (not a solid color) and used paint to save it in both formats. This time the jpeg was smaller than png. Is it solely due to the gradient of colors? if so then why?

Even if the picture is a solid color should jpg encodng be able to compress it even better?

like image 590
Sylviaa20 Avatar asked May 15 '17 01:05

Sylviaa20


2 Answers

It's to be expected that PNG performs better than JPEG in this scenario.

As pointed out in other answer, PNG does a per-line pixel prediction, followed by ZLIB compression. If the image has has a single colour, the prediction will produce a constant zero value for all the pixels, except for the start of each row. Hence the compression will be very effective. I'd bet that if the image were "landscape" (6552 x 15248 instead of 15248 x 6552) the compression would be even a little better.

JPEG compression, instead, divides the image in blocks of 8x8 pixels, and for each one it attempts to quantize finely the low frequency components and coarsely the high frecuency components. This works nicely for "natural" (photographic or rendered) images, but no so nicely for images with few colors (or a single one!).

See some comparisons here.

like image 97
leonbloy Avatar answered Oct 13 '22 00:10

leonbloy


Not necessarily.

PNG is a prediction-based algorithm, which means that it tries to deduct the value of one pixel based on previously coded pixels. I bet the prediction is really accurate for a solid image, thence the very good results.

JPEG accepts different "quality levels" which determine the size of your compressed file. The size differences between your experiment and the web version are likely due to that (unless you're downloading a different image, of course!).

Note that JPEG may introduce some image artifacts because it is a lossy algorithm, while PNG will recover the exact input image for you.

like image 21
mhernandez Avatar answered Oct 13 '22 00:10

mhernandez