将全彩色图像转换为三色图像以用于电子墨水显示

Convert full color image to three color image for e-ink display

我希望能够自动将全彩色图像转换为电子墨水显示器 (Waveshare 7.5") 的三种颜色(黑色/红色/白色)。现在我只是让屏幕处理它,但正如预期的那样,复杂的图像被冲掉了。

我可以应用任何算法或过滤器来让事情更明显吗?

目前我正在使用Python,但如果有必要,我不反对其他languages/environments。

好图:

图片褪色:

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


编辑:在 PIL(Python 成像库)的情况下,it doesn't seem trivial 抖动到任意一组三种颜色,至少从 2012 年开始。

您可以制作自己的 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

只是在 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魔杖。它基于 Imagemagick。

添加:

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

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

红色:

黑色:

只是对 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  -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 归因于此 - 谢谢!