减少 PNG 图像中的颜色会使文件大小变大

发布于 2024-10-02 12:43:56 字数 424 浏览 0 评论 0原文

我正在使用 ImageMagick 通过减少图像中的颜色以编程方式减小 PNG 图像的大小。我获取图像的 unique-colors 并将其除以 2。然后我将此值分配给 -colors 选项,如下所示:

variable = unique-colors / 2

Convert image.png -colors variable -depth 8

我认为这会大大减少图像的大小,但它增加了磁盘上的图像大小。任何人都可以阐明这一点。

谢谢。

编辑:事实证明问题是抖动。抖动可以帮助减少颜色的图像看起来更像原始图像,但会增加图像尺寸。要消除 ImageMagick 中的抖动,请在命令中添加 +dither。 示例

转换 CandyBar.png +dither -colors 300 -深度 8 lesserCandyBar.png

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安稳善良 2024-10-09 12:43:56

Imagemagick 可能使用一些抖动算法来使图像看起来好像具有原始颜色量。这增加了图像数据的“随机性”(单个像素在某些地方重新着色以混合成其他颜色)并且该图像数据也不再打包。进一步研究 convert 命令如何进行抖动。您还可以通过在 gimp/等效程序中添加第二个图像作为图层并调整透明度来看到此效果。

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.

少钕鈤記 2024-10-09 12:43:56

为此,您应该使用 pngquant

您无需猜测颜色数量,它具有实际的 --quality 设置:

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

上面将自动选择与 JPEG 质量相同的比例匹配给定质量所需的颜色数量(100 = 完美) ,70 = 好,20 = 糟糕)。

pngquant 具有更好的量化算法,量化越好,质量/文件大小比就越好。

而且 pngquant 不会对看起来不错但不抖动的区域进行抖动,这避免了向文件添加不必要的噪声/随机性。

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.

趴在窗边数星星i 2024-10-09 12:43:56

“新”PNG 的压缩效果不如原始压缩。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文