简体   繁体   English

像图像编辑器一样在ImageMagick中进行量化时,如何忽略透明度?

[英]How do I ignore transparency when quantizing in ImageMagick like image editors do?

I'm trying to reduce the number of colors in a png while ignoring the alpha channel to produce results similar to what I'd get in Photoshop or GIMP. 我正在尝试减少png中的颜色数量,同时忽略alpha通道以产生与Photoshop或GIMP中类似的结果。 I've read the ImageMagick quantize page, and tried out the various options it goes over to little success. 我已阅读ImageMagick 量化页面,并尝试了各种选项,但收效甚微。 ImageMagick continues to mess with the transparency even when I use the TransparentColorspace (-transparent) when quantizing. 即使我在量化时使用TransparentColorspace(透明),ImageMagick仍然会混乱透明性。

Album of Examples 范例集

Results similar to the second image are what I'm aiming for, which is what the first looks like after reducing the colors to 4 in GIMP (the transparency channel is preserved in the end result and didn't affect the resulting colors from quantization). 我的目标是获得类似于第二张图像的结果,这就是第一张在GIMP中将颜色减少到4种后的样子(透明通道保留在最终结果中,并且不影响量化产生的颜色) 。 The third and fourth are what ImageMagick produces with RGB and Transparent colorspace settings (large sections of the opaque areas are converted to transparent, and the 4th color is used for transparency; resulting colors are darker overall). 第三和第四种是ImageMagick在RGB和“透明”色彩空间设置下产生的结果(不透明区域的大部分被转换为透明,第四种颜色用于透明;结果颜色整体较暗)。

Splitting the channels and explicitly selecting the channels for which to reduce the colours, then re-combining channels, might be a way to go: 拆分通道并明确选择要减少色彩的通道,然后重新组合通道,可能是一种方法:

convert UmRGOCs.png -separate -channel RGB -colors 4 +channel -combine out.png

在此处输入图片说明

Working off of Mark Setchenll's answer , I noticed his command strips the transparency and replaces it with a color based on the image's contents but increases the color count. 根据Mark Setchenll的答案 ,我注意到他的命令剥离了透明度,并根据图像的内容将其替换为一种颜色,但增加了颜色计数。 Working from that, I found that overlaying the original image over the result allowed me to then reduce the color count without the resulting colors being noticeably different because of the transparency. 从那开始,我发现将原始图像覆盖在结果上可以使我减少颜色计数,而不会由于透明性而导致结果颜色明显不同。 The original alpha channel is then copied over from the original image to produce a result that is very similar to what is produced by GIMP. 然后从原始图像复制原始的Alpha通道,以产生与GIMP产生的结果非常相似的结果。

convert emblem.png ( +clone ( +clone -separate -channel RGB +dither -colors 4 +channel -combine ) +swap -flatten ) -channel A -threshold 50% +swap -compose CopyOpacity -composite +dither -colors 5 test.png

The final reduction is one larger to account for transparency, and produces a color palette similar in count and colors to those from image editors for this image. 最后的减少量要大得多,以解决透明度问题,并产生与数量和颜色相似的调色板,其颜色和数量与图像编辑器中的图像相同。

Comparison Images 比较图像

  1. Original 原版的
  2. GIMP GIMP
  3. convert UmRGOCs.png -separate -channel RGB -colors 4 +channel -combine out.png
  4. convert emblem.png ( +clone ( +clone -separate -channel RGB +dither -colors 4 +channel -combine ) +swap -flatten ) -channel A -threshold 50% +swap -compose CopyOpacity -composite +dither -colors 5 test.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM