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

时间:2019-05-05 01:22:58

标签: python colors raspberry-pi e-ink

我希望能够将全彩色图像自动转换为三种颜色(黑色/红色/白色)以显示电子墨水(Waveshare 7.5英寸)。现在,我只是让屏幕来处理它,但不出所料,复杂的图像被洗掉了。

有什么我可以应用的算法或过滤器使事情更明显?

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

良好的图像:

Good image

洗出的图片:

washed out

4 个答案:

答案 0 :(得分:1)

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


编辑:对于PIL(Python影像库),it doesn't seem trivial抖动为至少三种颜色的任意一组,至少从2012年开始。

答案 1 :(得分:1)

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

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

然后您可以将其应用于图像,如下所示:

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

enter image description here

如果要直接将其发送到帧缓冲区并且它支持RB888,则可以尝试运行以下内容:

magick input.png +dither -remap palette.gif -depth 8 RGB:/dev/fb0

答案 2 :(得分:1)

只需在Mark Setchell的答案中加上一点即可。对于打印,您可能最好抖动3种颜色。因此,这是使用Imagemagick 7进行抖动处理和不进行抖动处理的图像。如果使用Imagemagick 6,请用convert替换magick。

输入:

enter image description here

创建3个调色板:

to_sym


具有抖动功能(默认为Floyd-Steinberg):

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


enter image description here

没有抖动

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


enter image description here

如果您需要Python,则可以尝试使用Python Wand。它基于Imagemagick。

答案 3 :(得分:0)

只是在 Mark 和 Fred 的答案中添加一点。我在 Raspberry Pi 上使用 ImageMagick,它是版本 < 7 并使用“转换”。 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

我已经把马克和弗雷德都归功于那里 - 谢谢!

Example conversion of photograph to tri-color Floyd-Steinberg dithered image