Search code examples
graphicscolorspngimagemagickgraphicsmagick

Reducing colors in a PNG image is making the file size bigger


I am using ImageMagick to programmatically reduce the size of a PNG image by reducing the colors in the image. I get the images unique-colors and divide this by 2. Then I assign this value to the -colors option as follows:

variable = unique-colors / 2

convert image.png -colors variable -depth 8

I thought this would substantially reduce the size of the image but instead it increases the images size on disk. Can anyone shed any light on this.

Thanks.

EDIT: Turns out the problem was dithering. Dithering helps your reduced color images look more like the originals but adds to the image size. To remove dithering in ImageMagick add +dither to your command. Example

convert CandyBar.png +dither -colors 300 -depth 8 smallerCandyBar.png


Solution

  • Imagemagick probably uses some dithering algorithm to make image appear as though it has original amount of colors. This increases image data "randomness" (single pixels are recolored at some places to blend into other colors) and this image data no longer packs as well. Research further into how the convert command does the dithering. You can also see this effect by adding second image as a layer in gimp/equivalent program and tuning transparency.