Thursday, March 6, 2008

mogrified...

JPEG image compression: Single Step vs Multiple Steps

I got a chance to test this thing in one of my short projects. and the findings were worth noticing.
So, first start with formulating the problem, the problem was:
"Is it better to compress a jpg image in more than one step than in a single step? For example, if I want to compress an image upto 60% then I can do it in a single step i.e. just reduce the quality down to 60% or in multiple steps i.e. first reduce the quality down to 80% then reduce it further to 75% (simple percentage rule 75% of 80% = 60%)."
So which one is better? In order to find out, I collected some sample jpg's from various sources (based on resolution, content present in the image etc) and run the test using open source Imagemagick (open source linux library for images). So I used mogrify (command line tool from the same library) for my task and the results were quite noticeable.
For all images, single step compression was doing better than the later one. And with some particular type of images, there was considerable difference in output sizes (i.e. from single step and multiple steps). These image were large paintings, in which color gradients were smaller (or in other words lesser color-flip boundaries) in comparison to a screenshot of your desktop with 5-6 windows open.
Single step compression implies, compressed image data with some tables (which jpeg library uses to compress and decompress the image) which is an overhead.
Multiple steps compression implies, more number of overheads and each overhead introduces complexity in next compression step.
So the bottom line is: Avoid compressing images in steps (for whatever reasons you want to do it in this way), just do it in a single step... :-)

2 comments:

Anonymous said...

Frankly this problem has existed for usall along, but only now do we understad this ...

Any clues how other conversion systems are handling this.. e.g. flash paper, pdf writers etc


amit

bhups said...

Regarding image compression (JPG images), it all depends on the jpeg library used by any of the conversion systems. And as jpeg is an open standard, anybody can write his own library and handles the things accordingly. I used Imagemagick for this which I've mentioned.
If I get some more information regarding this I'll post the same...
cheers!
bhups