需要帮助理解 ImageMagick/Graphicsmagick -colors 选项

发布于 2024-10-03 17:28:04 字数 310 浏览 6 评论 0原文

我正在尝试以编程方式减少(有损)PNG 和 GIF 文件的文件大小。作为其中的一部分,我需要减少图像中的颜色数量。我不想将所有图像减少为单一颜色值,所以我正在做的是;获取图像中独特颜色的数量;将此数字除以 2 可将颜色数量减少一半。

问题是这行不通。使用 ImageMagic 速度太慢,并且不会减小文件大小,除非图像具有几百种独特的颜色。无论原始图像中有多少种颜色,使用 GraphicsMagick 始终会产生低于 255 的唯一颜色值。 GraphicsMagick 的另一个问题是,如果图像中存在任何透明像素,它会用透明替换丢失的颜色。

任何帮助将不胜感激, 谢谢。

I am trying to programmatically reduce (lossy) the file size of PNG and GIF files. As part of this I need to reduce the number of colors in the images. I don't want to reduce all the images to a single colors value, so what I am doing is; get the number of unique colors in the image, then; divide this number by 2 to reduce the number of colors by half.

The problem is this does not work. Using ImageMagic it is way too slow and doesn't reduce the file size unless the image has under a few hundred unique colors. Using GraphicsMagick always results in a unique colors value under 255 regardless of how many colors were in the original image. Another problem with GraphicsMagick is if there are any transparent pixels in the image it replaces the lost colors with transparent.

Any help would be gratefully welcome,
Thanks.

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

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

发布评论

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

评论(3

南薇 2024-10-10 17:28:04

减少颜色数量

  1. 仅当图像可以使用调色板而不是存储每个像素的颜色时,
  2. 才有用调色板索引的大小小于颜色的大小
  3. 图像格式支持调色板大小

我认为你只能获得 1 位、4 位、8 位,以及这些格式的 2 色、16 色或 256 色。我认为如果您要求更多,您只会被截断为 256。如果您要求更少,它只是不会使用整个调色板。

您是否考虑过转换为 JPEG 并使用质量设置?您最终可以对有损进行更精细的控制。缺点是,如果图像不是照片,但听起来它们有很多颜色,所以它们可能是。

如果接近您想要的,则可以选择 1、4、8 位;如果有很多颜色,则可以选择 jpeg。

Reducing the number of colors is only useful if

  1. the image can then use a palette instead of storing the color for each pixel
  2. the size of a palette index is smaller than the size of a color
  3. the image format supports the palette size

I think you can only get 1-bit, 4-bit, 8-bit, so 2-color, 16-color, or 256-color in those formats. I think if you ask for more, you just get truncated to 256. If you ask for less, it just doesn't use the entire palette.

Have you considered converting to JPEG and playing with the quality setting? You end up with more fine grain control of lossy-ness. The drawback is if the images aren't photos, but it sounds like they have a lot of colors, so they might be.

Perhaps choose 1, 4, 8 bit if it's close to what you want and jpeg if it has a lot of colors.

不必你懂 2024-10-10 17:28:04

我认为您所追求的 ImageMagick 设施可能是量化的:

http://www.imagemagick.org/Usage/量化/

I think the ImageMagick facility you are after might be quantization:

http://www.imagemagick.org/Usage/quantize/

ι不睡觉的鱼゛ 2024-10-10 17:28:04

第一个问题,GraphicsMagick 可以使用 8 位、16 位或 32 位量子级别进行编译。我的版本被编译成8位(默认),这意味着可以分配给图像的最大颜色数是256种独特的颜色(3 * 3 * 2,其中1个蓝色位被删除,因为人眼可以没有正确看到它)。显然,GraphicsMagick 可以处理比这更多颜色的图像,但在减少颜色时,它只能减少到 256 种或更少的颜色。较大的像素量会导致 GraphicsMagick 运行速度更慢并且需要更多内存。例如,使用 16 位像素量子导致 GraphicsMagick 的运行速度比支持 8 位像素量子时慢 15% 到 50%(并且占用两倍的内存)。

第二个问题; PNG图像中的透明度处理,我使用的是早期版本的GraphicsMagick(我认为是1.1),无论如何,当我升级到1.3时,这个问题不再存在,这告诉我这是GraphicsMagick 1.1中的一个错误导致了这个问题。

First problem, GraphicsMagick can be compiled using 8 bit, 16 bit or 32 bit quantum levels. My version is compiled into 8 bit (the default), this means that the max number of colors that can be assigned to an image is 256 unique colors (3*3*2, 1 of the blue bits is removed because the human eye can't see it properly). Obviously, GraphicsMagick can handle images with more colors than this but when reducing colors it can only reduce to 256 or less colors. Larger pixel quantums cause GraphicsMagick to run more slowly and to require more memory. For example, using sixteen-bit pixel quantums causes GraphicsMagick to run 15% to 50% slower (and take twice as much memory) than when it is built to support eight-bit pixel quantums.

Second problem; transparency handling in PNG images, I was using an earlier version of GraphicsMagick (1.1 I think), anyway, when I upgraded to 1.3 this problem is no longer present which tells me that it was a bug in GraphicsMagick 1.1 that caused this.

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