Wednesday, March 26, 2008

magick... magick... everywhere...

Recently I had a problem while converting a 'png' image into 'jpeg' format. I was using Imagemagick (mogrify command line tool) for this. I used the following command:

%mogrify -format jpg pic.png

Converted 'jpeg' image was totally different from the original 'png' image. Some area of the converted 'jpeg' image was coming black instead of white in the original one.



Then I tried the same thing using GIMP, and I came to know that 'png' format supports layers. So flattening is needed before converting a 'png' image into 'jpeg' because later one does not support layers. So I tried converting the image using following parameters and the result was as good as I wanted it to be :).

%mogrify -format jpg -layers flatten pic.png

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... :-)