What options for convert (ImageMagick or GraphicsMagick) produce the smallest (filesize) PNG?

Jonah Braun picture Jonah Braun · Nov 19, 2010 · Viewed 16.5k times · Source

ImageMagick creates some pretty large PNGs. GraphicsMagick is a lot better, but I'm still looking for the best options to use with convert to obtain the smallest filesize png.

I have here a large png with a small filesize, and passing this through IM convert I have been unable to reach that filesize, let alone get it smaller. With GM convert I can get it slightly smaller but I'm looking for improvements, generically for any image I come across.

gm convert -quality 95 a_png.png gm.png
convert -quality 95 -depth 8 a_png.png im.png
gm identify *

a_png.png PNG 2560x2048+0+0 PseudoClass 256c 8-bit 60.1K 0.000u 0:01
gm.png[1] PNG 2560x2048+0+0 PseudoClass 256c 8-bit 60.0K 0.000u 0:01
im.png[2] PNG 2560x2048+0+0 DirectClass 8-bit 130.2K 0.000u 0:01 

What options for convert produce the smallest PNG filesize?

(Yes, I'm familiar with OptiPNG, PNGOUT and Pngcrush. But I'm after something that will be available without question on every *nix box I happen to be on.)

Answer

toc777 picture toc777 · Nov 24, 2010

Looks like you and me are looking for the same answer. Unfortunately there doesn't seem to be many people out there with a good knowledge of GraphicsMagick. This is what I have learned so far,

The quality operator doesn't properly work for any image other than JPEG's. For me it just made the file size bigger when used on PNG's and GIF's.

I have done this to my PNG and GIF files to reduce their size:

gm convert myImage.png +dither -depth 8 -colors 50 myImage.png
  • +dither stops any dithering of the image when the colors are reduced. (this reduces the file size)

  • -depth 8 is probably unnecessary as most PNG files are already depth 8.

  • -colors 50 reduces the number of colors in the image to 50, this is the only way to really reduce the size of a image stored in a lossless format like PNG or GIF.

Obviously for the best image quality/size ratio you cant just reduce the image depth or number of colors without knowing the current depth and number of colors. To determine this information I am doing the following

gm identify -format "file_size:%b,unique_colors:%k,bit_depth:%q" myImage.png

For my image; this returns

file_size:100.7k,unique_colors:13455,bit_depth:8

The problem is when GraphicsMagick reduces colors it always reduces to at least 255, so you can't set the number of colors to 300 for example. Also there seems to be an issue with the alpha channel for PNG files; If the image has transparency in it, reducing colors replaces these colors with transparent; with imagemagick it does not do this.