【问题标题】:Convert full color image to three color image for e-ink display将全彩图像转换为三色图像以进行电子墨水显示
【发布时间】:2019-09-23 01:57:15
【问题描述】:

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

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

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

好形象:

洗掉的图片:

【问题讨论】:

  • 在此处查看我的答案stackoverflow.com/a/55965617/2836621,但只需将红色、黑色和白色放入您的调色板,而不是我使用的 8 种颜色。
  • 您需要更具体地了解如何写入显示器...您是打开帧缓冲区并向其写入原始字节还是有一些 API。此外,您如何以及以何种格式接收图像?该显示器肯定可以显示 65,536 多种颜色吗?
  • 我只是使用PIL 并创建一个图像缓冲区,没什么特别的。
  • 另外@MarkSetchell,显示器是三色电子墨水显示器,所以它唯一能做的颜色是红色、白色和黑色。中间没有阴影。
  • 目前看来,您的ImageMagick 调用似乎运作良好。我可能只是通过管道调用一个子进程调用来运行它,因为添加了新图像......如果你想添加它作为我会接受的答案。

标签: python colors raspberry-pi e-ink


【解决方案1】:

只是在 Mark 和 Fred 的答案中添加一点内容。我在 Raspberry Pi 上使用 ImageMagick,它的版本

# 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 都归功于那里 - 谢谢!

【讨论】:

    【解决方案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。

    补充:

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

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

    红色:

    黑色:

    【讨论】:

    • 谢谢你,弗雷德。我没有涉及抖动,因为它已经在我在 cmets 中链接到的答案中,但这是一个有效的观点。
    • 好东西!你如何将它分成两张图片——一张是红色的,一张是黑色的? waveshare 需要这样做,这是此处原始问题的一部分。还没有找到答案,所以我认为这是一个提出这个问题的好地方。
    • 我不会从您的问题中解释这一点,请进一步解释。如果拆分为红色,什么显示没有红色的地方?我的图像只有您要求的 3 种颜色——红色、黑色、白色。
    • 此处处理的图像如预期的那样呈现出红色、白色和黑色的阴影。 Waveshare 电子纸将其渲染为两张图像 - 一张用于黑色部分,一张用于红色部分。因此,需要从该图像生成两幅图像——一幅具有红色像素,另一幅具有黑色像素。然而,目前尚不清楚如何使用 imagemagick 或直接使用 Pillow 和 Python 操作来实现这一点。当我查看图像的像素数据时,它仍然是 RGB 的变体——例如 (173, 133, 134)。所以不能一味的取出红色像素或者黑色像素。
    • 如果为新图像提取红色像素,白色和黑色像素会发生什么变化?它们会变成黑色还是透明?您不能拥有只有一些像素的图像。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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


      编辑:在 PIL(Python 图像库)的情况下,it doesn't seem trivial 至少在 2012 年时抖动为任意三种颜色。

      【讨论】:

        猜你喜欢
        • 2015-05-28
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-26
        • 2021-05-01
        • 2011-12-28
        • 1970-01-01
        相关资源
        最近更新 更多