2

我希望能够自动将全彩色图像转换为三色(黑色/红色/白色)以用于电子墨水显示器(Waveshare 7.5")。现在我只是让屏幕处理它,但作为预期的复杂图像会被洗掉。

有没有我可以应用的算法或过滤器让事情变得更明显?

现在我正在使用 Python,但如有必要,我并不反对其他语言/环境。

好图:

好形象

洗掉的图像:

淘汰

4

4 回答 4

2

您可以制作自己的 3 种可接受颜色的调色板,如下所示:

magick xc:red xc:white xc:black +append palette.gif

然后你可以像这样将它应用到你的图像中:

magick input.png +dither -remap palette.gif result.png

在此处输入图像描述

如果你想直接将它发送到帧缓冲区并且它支持 RB888,你可以尝试运行这样的东西:

magick input.png +dither -remap palette.gif -depth 8 RGB:/dev/fb0
于 2019-05-07T16:17:46.970 回答
2

只需在 Mark Setchell 的回答中添加一点内容。对于打印,您可能会更好地抖动 3 种颜色。因此,这是您使用 Imagemagick 7 进行抖动和不进行抖动的图像。如果使用 Imagemagick 6,请将 magick 替换为 convert。

输入:

在此处输入图像描述

创建 3 个调色板:

magick xc:red xc:white xc:black +append palette.gif

使用抖动(默认为 Floyd-Steinberg):

magick input.png -remap palette.gif result.png

[![在此处输入图像描述][2]][2]

没有抖动

magick input.png -dither none -remap palette.gif result2.png

[![在此处输入图像描述][3]][3]

如果你想要 Python,那么你可以试试 Python Wand。它基于 Imagemagick。

添加:

要将红色和黑色分成两个图像,每个图像用黑色表示,其余的用白色表示,您可以执行以下操作并在评论中根据需要保存为 BMP。(您可以根据需要从上方抖动或不抖动)

magick result.png -color-threshold "red-red" -negate red.bmp
magick result.png -color-threshold "black-black" -negate black.bmp

红色的:

在此处输入图像描述

黑色的:

在此处输入图像描述

于 2019-05-12T02:23:43.527 回答
1

您似乎正在为每个像素选择最接近的颜色。看看抖动算法是否更适合您的目的。通常,抖动算法在确定如何为给定像素着色时会考虑相邻像素。


编辑:在 PIL(Python 成像库)的情况下,至少在 2012 年,抖动到任意三种颜色的集合似乎并不容易。

于 2019-05-05T09:15:04.193 回答
1

只需在 Mark 和 Fred 的答案中添加一些内容。我在 Raspberry Pi 上使用 ImageMagick,它的版本 < 7 并使用“convert”。Fred 建议的某些命令不适用于该版本。这是我调整大小、重新映射和抖动并将图像拆分为黑白和黑白红子图像的操作。

# Create palette with red, white and black colors
convert xc:red xc:white xc:black +append palette.gif

# Resize input file into size suitable for ePaper Display - 264x176
# Converting to BMP.
# Note, if working with JPG, it is a lossy
# format and subsequently remapping and working with it results
# in the color palette getting overwritten - we just convert to BMP
# and work with that instead
convert $1 -resize 264x176^ -gravity center -extent 264x176 resized.bmp

# Remap the resized image into the colors of the palette using
# Floyd Steinberg dithering (default)
# Resulting image will have only 3 colors - red, white and black
convert resized.bmp -remap palette.gif result.bmp


# Replace all the red pixels with white - this
# isolates the white and black pixels - i.e the "black"
# part of image to be rendered on the ePaper Display
convert -fill white -opaque red result.bmp result_black.bmp 

# Similarly, Replace all the black pixels with white - this
# isolates the white and red pixels - i.e the "red"
# part of image to be rendered on the ePaper Display
convert -fill white -opaque black result.bmp result_red.bmp

我还使用 Python Wand 实现,这是 ImageMagick 上的 Python 层

# This function takes as input a filename for an image
# It resizes the image into the dimensions supported by the ePaper Display
# It then remaps the image into a tri-color scheme using a palette (affinity)
# for remapping, and the Floyd Steinberg algorithm for dithering
# It then splits the image into two component parts:
# a white and black image (with the red pixels removed)
# a white and red image (with the black pixels removed)
# It then converts these into PIL Images and returns them
# The PIL Images can be used by the ePaper library to display
def getImagesToDisplay(filename):
    print(filename)
    red_image = None
    black_image = None
    try:
        with WandImage(filename=filename) as img:
            img.resize(264, 176)
            with WandImage() as palette:
                with WandImage(width = 1, height = 1, pseudo ="xc:red") as red:
                    palette.sequence.append(red)
                with WandImage(width = 1, height = 1, pseudo ="xc:black") as black:
                    palette.sequence.append(black)
                with WandImage(width = 1, height = 1, pseudo ="xc:white") as white:
                    palette.sequence.append(white)
                palette.concat()
                img.remap(affinity=palette, method='floyd_steinberg')
                
                red = img.clone()
                black = img.clone()
                red.opaque_paint(target='black', fill='white')
                # This is not nececessary - making the white and red image
                # white and black instead - left here FYI
                # red.opaque_paint(target='red', fill='black')
        
                black.opaque_paint(target='red', fill='white')
                
                red_image = Image.open(io.BytesIO(red.make_blob("bmp")))
                black_image = Image.open(io.BytesIO(black.make_blob("bmp")))
    except Exception as ex:
        print ('traceback.format_exc():\n%s',traceback.format_exc())

    return (red_image, black_image)

这是我关于 Hackster 的项目的文章(包括完整的源代码链接) - https://www.hackster.io/sridhar-rajagopal/photostax-digital-epaper-photo-frame-84d4ed

我把 Mark 和 Fred 都归功于那里——谢谢!

将照片转换为三色 Floyd-Steinberg 抖动图像的示例

于 2021-04-21T23:02:12.627 回答