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

时光怂恿深爱的人放手 提交于 2020-01-06 02:37:18

问题


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


回答1:


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.




回答2:


You should use pngquant for this.

You don't need to guess number of colors, it has actual --quality setting:

pngquant --verbose --quality=70 image.png

The above will automatically choose number of colors needed to match given quality in the same scale as JPEG quality (100 = perfect, 70 = OK, 20 = awful).

pngquant has substantially better quantization algorithm, and the better the quantization the better quality/filesize ratio.

And pngquant doesn't dither areas that look good without dithering, and this avoids adding unnecessary noise/randomness to the file.




回答3:


The "new" PNG's compression is not as good as the one of the original.



来源:https://stackoverflow.com/questions/4203823/reducing-colors-in-a-png-image-is-making-the-file-size-bigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!