14

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

Jonah Braun
  • 4,155
  • 7
  • 29
  • 31
  • 1
    The ones that turns it into a 1x1 PNG8. – Ignacio Vazquez-Abrams Nov 19 '10 at 18:08
  • When you can execute scripts on a machine, you can also put files on it, right? How about linking pngcrush statically and putting it on the *nix box you're using? – thejh Nov 19 '10 at 18:38
  • 1
    @thejh, yes I could do that. That's often not acceptable in production environments however. Besides, IM/GM together are likely the most popularly used graphics packages. So I'm sure it's just a matter of finding the right options... – Jonah Braun Nov 19 '10 at 19:05
  • Gimp produces really small pngs (often better than PNGOUT and PNGCrush). But probably it's not the best solution if you need high performance, do you? – pmoleri Nov 21 '10 at 03:09
  • Yep, thanks for the suggestion, but Gimp is just a little difficult to script/deploy on headless servers. :) – Jonah Braun Nov 22 '10 at 19:39
  • Hi Jonah, I have run into a problem with GraphicsMagick1.3.* compressing PNG files. Some of theses PNG files are being doubled in size for no apparent reason. I was wondering if you have encountered this issue? – toc777 Feb 17 '11 at 11:25

3 Answers3

13

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.

David Foerster
  • 1,461
  • 1
  • 14
  • 23
toc777
  • 2,607
  • 2
  • 26
  • 37
  • Also for JEPGS, you can add +profiles "*" to remove any profiles from the image, this will reduce the image size slightly. I think because we are both trying to do the same thing we could share knowledge on this issue. If you find additional information or have any questions, message me. thanks. – toc777 Nov 24 '10 at 12:58
  • Thanks for the informative post toc777. One correction: -quality definitely makes a difference on the filesize of PNGs but it does a different operation than for jpeg, read the spec for details, I have found -quality 95 to make the smallest png. Here's a bash one liner to demonstrate: for i in {1..100}; do gm convert big_yet_small.png -quality $i $i.png; done – Jonah Braun Nov 26 '10 at 21:28
  • We have also run into the GM transparency bug. It appears to trigger on larger images, sizing down with IM first and then processing with GM is a work around. Need to file a bug report... – Jonah Braun Nov 26 '10 at 21:30
  • 1
    Hey Jonah, I was doing the same thing but then found out that this issue is fixed in the newer GraphicsMagick(1.3). Well its fixed for all the images I have been testing on. I'm guessing this bug is fixed but I cant find any bug reports on it. – toc777 Dec 01 '10 at 16:56
  • For more info check out my answer http://stackoverflow.com/questions/4217960/need-help-understanding-the-imagemagick-graphicsmagick-colors-option – toc777 Dec 01 '10 at 17:04
  • What kind of PNG image are you using? On any of my test images -quality 95 makes them slightly bigger. – toc777 Dec 01 '10 at 17:12
7

I just came across this question again so I'll update, GraphicsMagick and ImageMagick have a serious problem. They cannot write out PNG images using a tRNS chunk which means if you try to read in an image that has a tRNS chunk and then write it out, the image will be much bigger. GM is not the best tool for compressing images. You need to use a separate tool such as OptiPNG to compress PNG's again after using Image/GraphicsMagick. I am getting up to 60% smaller files when using OptiPNG after running GraphicsMagick on an image.

Also I was wondering if you have encountered a problem regarding RGBA images and bit depth. For some images I am getting an "Invalid bit depth" exception. I can't see any reason why.

toc777
  • 2,607
  • 2
  • 26
  • 37
3

I haven't found a way to do it in the command line, but i did find this free website (http://tinypng.org/) that does an excellent job, my test image got a 71% reduction, final size was only 29% of the original. It looks like you can give it 20 images at a time. I'm looking into how they do it.

http://tinypng.org/

23inhouse
  • 1,889
  • 19
  • 18