Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "convert x.png y.png" creates an image with a different size (KB)?

Tags:

imagemagick

I have a few images I'm trying to normalize to a certain scale by resizing them. When I started resizing the images, I noticed that their volume (KB) became much larger than the original image.

I checked it out further by simply doing: convert x.png y.png And got some difference. E.g. in one case x.png is 143KB and y.png is 208KB.

Since I'm also optimizing for image size, I was wondering if anyone has an idea why this is the case and any suggestions to solve this.

I'm using Imagemagick 6.6.4-Q16 on windows 7.

Thanks, Amit

EDIT:

Thanks for the suggestion, guys, and sorry for the late reply! Here's what I tried to do and here are the results. The original image is 112KB. Doing convert with no parameters results in 169KB image. Using OptiPNG on it (PNGCrush didn't work for me) with -o7 results in 113KB. I then tried to do convert -quality X for every X between 0-99, and I found that for quality 90 I achieve a file size of 116KB, so OptiPNG is better. However - when I do convert -resize to a smaller image (proportionally), I get a 199KB with no parameters, 196KB for the best convert -quality X (x=0), and 196KB for OptiPNG. This is 75% increase in file size when I would expect the file to be smaller, since I resized the image by 35% from 377x2290 to 234x1419. Any thoughts?

Thanks!

EDIT - Images attached + the resize problem investigated:

Here is a link to the three images (look at the captions): Original image, image after simple ImageMagick convert, and the same output image after optipng. http://picasaweb.google.com/101622787022351879933/ImageMagickProblem?feat=directlink

Also, I think there might be an issue with the resize option of ImageMagick. I did the following exercise to isolate it: For every x between 1-100: convert a.png -resize x% a_x.png

As expected, the size of the image almost constantly increases from 1% to 100%. There is a slight drop in 40% and in 50%. The issue is that from 40% and on, the output image size bypasses the original image size. Only when you hit 100% there is a large drop in image size back to the original size (slightly more actually, per my first edit - 169KB instead of 112KB, and 113KB after Optipng).

Even running Optipng with -o7 on each output file doesn't correct this.

You can see the results of this experiment in the graph below: http://picasaweb.google.com/lh/photo/fFL1W6SiUfFWhBqdUf3BZg?feat=directlink

like image 263
AmitA Avatar asked Jan 22 '23 09:01

AmitA


2 Answers

PNGs have many different settings affecting the compression, and there's no guarantee that the default ImageMagick settings choose a particular efficient compression.

The easiest solution is to run your PNG through OptiPNG after ImageMagick is done with them. By default, it should generate something close to the best possible compression, but you can ask it to do a more exhaustive testing to get to an absolute minimum.

Regarding your edit: One thing you have to be aware of here is the resize method. ImageMagick will use a filter during the resize, and that can have a big impact on how much you can compress the image with PNG: after all, PNG is lossless compression, so it can't just approximate it like JPEG.

Which filters will work for you really depends on what sort of image you have, but you can try various options and see what looks nice. You might need different filters for different images.

like image 115
Michael Madsen Avatar answered May 19 '23 03:05

Michael Madsen


Ok, the issue was with the filters per Michael's suggestion.

Here is what I did:

convert -list filter > 1.txt

to get a list of all filters. Then, for every x which is a filter on that list, I did:

convert -filter x -resize 234x1419 a.png b_x.png

I recorded the file size, and then did "optipng -o7" on each file.

The best filter for this image for my case was "Box": it was the only one that actually got reduced from 112KB to 111KB, and then to 81KB by optipng.

I think it is interesting to note that after all this process, only the Box filter actually got the size of the image reduced, even though all of them have less pixels after the resize process.

BTW: watch out for the following trap

convert -filter Box -resize 234x1419 a.png a_box.png

results in an image of 112KB which can be compressed to 81KB, as I said above. However:

convert a.png -filter Box -resize 234x1419 a_box.png

results in an image of 199KB!

like image 30
AmitA Avatar answered May 19 '23 03:05

AmitA