Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB


Steve on Image Processing with MATLAB has been archived and will not be updated.

JPEG and PNG – lossy and lossless image compression

I was reviewing enhancement requests recently when I came across this one: Add support for a 'Quality' parameter when using imwrite to write a PNG file, just like you can currently do when writing a JPEG file.

Well, there is actually a pretty good reason why there is no 'Quality' parameter for writing PNG files, but it's not an obvious reason unless you know more about the differences between various image compression methods.

A very important characteristic of a compression method is whether it is lossless or lossy. With a lossless compression method, the original data can be recovered exactly. When you make a "zip" file on your computer, this is what you certainly expect.

Here's an example: I can use gzip to compress the MATLAB script file I'm using for this blog post.

lossless_lossy.m     lossless_lossy.m.gz  

Let's compare their sizes to see if the output of gzip is really compressed.

d1 = dir('lossless_lossy.m');
ans =


d2 = dir('lossless_lossy.m.gz');
ans =


Indeed, the compressed does actually have fewer bytes. Can we get the original file back exactly?

ans =



Let's switch to image formats. The PNG image format uses lossless compression. When you save image data to a PNG file, you can read the file back in and get back the original pixels, unchanged. For a sample image I'll use my imzoneplate function on the MATLAB Central File Exchange.

I = im2uint8(imzoneplate);

Let's write I out to a PNG file, read it back in, and see if the pixels are the same.

I2 = imread('zoneplate.png');
ans =


OK, now let's try the same experiment using JPEG.

I2j = imread('zoneplate.jpg');
ans =


No, the pixels are not equal! It turns out the JPEG is a lossy image compression format. (Full disclosure: there is a lossless variant of JPEG, but it is rarely used.)

Why in the world would we use a compression format that doesn't preserve the original data? Because by giving up on exact data recovery and by taking advantage of properties of human visual perception, we can make the stored file a lot smaller. Let's compare the file sizes of the PNG file with the JPEG file.

z1 = dir('zoneplate.png');
num_bytes_png = z1.bytes
num_bytes_png =


z2 = dir('zoneplate.jpg');
num_bytes_jpeg = z2.bytes
num_bytes_jpeg =


The JPEG file is only one-third the size of the PNG file! But it looks almost exactly the same.


So, what about that 'Quality' parameter that I mentioned at the top of today's post? It turns out that we can make the JPEG file even smaller if we are willing to put up with some visible distortion in the image. Let's try a quality factor of 25. (The default is 75 on a scale of 0 to 100.)

I2j_25 = imread('zoneplate_25.jpg');

You can see some distortion, especially around the high-frequency part of the pattern. How about the file size?

z3 = dir('zoneplate_25.jpg');
ans =


That's about 54% of the size of the zoneplate.jpg.

Sometimes people think they can get "lossless JPEG" by using a 'Quality' factor of 100, but unfortunately that isn't the case. Let's check it:

I2j_100 = imread('zoneplate_100.jpg');
ans =


So there you go -- that's why there's no 'Quality' parameter when writing PNG files. PNG files are always perfect!

When I write blog posts, sometimes I use PNG image files and sometimes I use JPEG. My choice is based on what kind of graphics I have in that particular post. And that's a blog topic for another day.

Published with MATLAB® R2013a

  • print


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.