Linux 通过控制台使用自定义调色板将图像转换为索引颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7609210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:26:11  来源:igfitidea点击:

Convert image to indexed color with custom palette through console

linuxconsoleimagemagickgimp

提问by Smirnov

I have image.png in truecolor, palette.png (N colors, where N>256) or text file, where list RGB color palette. How to get a picture with this palette?

我有真彩色的 image.png、palette.png(N 种颜色,其中 N>256)或文本文件,其中列出了 RGB 调色板。如何使用此调色板获取图片?

If I use imagemagick:

如果我使用 imagemagick:

convert image.png -remap palette.png remap_image.png

It does not work.

这是行不通的。

convert image.png -map palette.png remap_image.png

Gives a very bad quality. The image is very noisy. File size is bigger than before.

质量很差。图像非常嘈杂。文件大小比以前更大。

GIMP gives best quality:

GIMP 提供最佳质量:

Сonvert image to indexed color > use custom palette

But GIMP is GUI. I need to convert a lot images in the console without running the gimp and X.org.

但是 GIMP 是 GUI。我需要在控制台中转换很多图像而不运行 gimp 和 X.org。

回答by gamen

Using a shared palette across multipleimages requires a carefully crafted palette. If you don't take great care when using the palette of a single image across many images, the result will be poor.

多个图像使用共享调色板需要精心设计的调色板。如果在多个图像中使用单个图像的调色板时不十分小心,结果将会很差。

This needn't be complicated though. If you have accesss to the GIMP (or other tool) which supports truecolor graphics, you can create a large image and fit all of the smaller images into it, then quantize the image to N colors, then use that palette as the source.

不过这并不复杂。如果您可以访问支持真彩色图形的 GIMP(或其他工具),您可以创建一个大图像并将所有较小的图像放入其中,然后将图像量化为 N 种颜色,然后使用该调色板作为源。

you should be able to closely mimic GIMP's behavior in the console using ImageMagick

您应该能够使用 ImageMagick 在控制台中密切模仿 GIMP 的行为

Once you've got a truecolor image with all the colors you want to quantize,

一旦你得到一张包含你想要量化的所有颜色的真彩色图像,

# Create an 8-bit png from our source, with a 235-color palette as an example.
convert truecolor_source.png -colors 235 palette.png

# Create an 8-bit png from an arbitrary image and use the palette in palette.png
convert sample.png -map palette.png output.png

There are a number of options for down-sampling colors, like dithering. See the ImageMagickv6example page for an excellent overview with example pictures and code.

有许多下采样颜色的选项,例如dithering。有关示例图片和代码的出色概述,请参阅ImageMagickv6示例页面。

Although I still don't exactlyunderstand what you want to do, your currently most recent comment ("Yes, from RGB to palette will set independently. Need set correct quantity of colors"), it sounds like all you want to do is set a strict limit on the amount of colors of a bunch of images, but they don't need to use the samepalette.

虽然我仍然不完全明白你想要做什么,但你最近的评论(“是的,从 RGB 到调色板将独立设置。需要设置正确的颜色数量”),听起来你想要做的就是设置对一堆图像的颜色数量有严格限制,但它们不需要使用相同的调色板。

In that case, the solution is very simple:

在这种情况下,解决方案非常简单:

convert sample.png -colors 135 output.png

Try playing with the quantization options if the result isn't to your satisfaction.

如果结果不让您满意,请尝试使用量化选项。

If the output image is too large for your liking, you can experiment with the -qualityoption.

如果输出图像太大而不符合您的喜好,您可以尝试使用该-quality选项。

If this still isn't satisfactory, please try to explain your goal in a more detailed manner. Good luck!

如果这仍然不令人满意,请尝试以更详细的方式解释您的目标。祝你好运!

回答by guadafan

cat photo.png | pngnq -s 1 > photoindexed.png

猫照片.png | pngnq -s 1 > photoindexed.png

回答by Bim

I tend to get good results with the "-remap" (single imge) or "+remap" (multiple images) functions in combination with "-colors". Read up on those functions here. Note that "with "-remap" you provide IM with the final set of colors you want to use for the image, whether you plan to dither those colors, or just replace the ones with their nearest neighbours.", meaning just remapping/replacing might not look good enough, as colors from the input image are simply replaced by those from the palette image. Some form of dithering will be necessary to distribute pixel color conversion errors throughout the output image, because not all colors in the palette match those of the input image.
I'd suggest you use the "-colors N" option for that. This will reduce your output image color count to 64. By default ImageMagick uses "-dither Riemersma" for this implicitly when you specify "-colors N". The are also otherditheringoptionsavailable.

我倾向于将“-remap”(单幅图像)或“+remap”(多幅图像)功能与“-colors”结合使用,以获得良好的效果。在这里阅读这些功能。请注意,“使用“-remap”向 IM 提供要用于图像的最终颜色集,无论您是打算抖动这些颜色,还是只是用它们最近的邻居替换这些颜色。”,这意味着只是重新映射/替换可能看起来不够好,因为来自输入图像的颜色被简单地替换为来自调色板图像的颜色。为了在整个输出图像中分布像素颜色转换错误,需要某种形式的抖动,因为并非调色板中的所有颜色都与输入图像的颜色相匹配。
我建议您为此使用“-colors N”选项。这会将您的输出图像颜色计数减少到 64。默认情况下,当您指定“-colors N”时,ImageMagick 会隐式使用“-dither Riemersma”。还有其他可用的抖动选项