【问题标题】:overlay an image and show lighter pixel at each pixel location覆盖图像并在每个像素位置显示较亮的像素
【发布时间】:2019-09-23 20:40:44
【问题描述】:

我有两张黑白图像,我想将它们与最终图像合并,显示两个图像中每个像素位置的较亮/白色像素。我尝试了以下代码,但没有成功。

background=Image.open('ABC.jpg').convert("RGBA")
overlay=Image.open('DEF.jpg').convert("RGBA")
background_width=1936
background_height=1863
background_width,background_height = background.size
overlay_resize= overlay.resize((background_width,background_height),Image.ANTIALIAS)
background.paste(overlay_resize, None, overlay_resize)
overlay=background.save("overlay.jpg")
fn=np.maximum(background,overlay)
fn1=PIL.Image.fromarray(fn)
plt.imshow(fnl)
plt.show()

我得到的错误信息是无法处理这种数据类型。任何人可以提供的任何帮助或建议都会很棒。

【问题讨论】:

    标签: python image-processing python-imaging-library


    【解决方案1】:

    我认为你把事情复杂化了。您只需要读取两个图像并将它们设为灰度numpy 数组,然后在每个位置选择两个像素中较亮的那个。

    所以从这两张图片开始:

    #!/usr/local/bin/python3
    
    import numpy as np
    from PIL import Image
    
    # Open two input images and convert to greyscale numpy arrays
    bg=np.array(Image.open('a.png').convert('L'))
    fg=np.array(Image.open('b.png').convert('L'))
    
    # Choose lighter pixel at each location
    result=np.maximum(bg,fg)
    
    # Save
    Image.fromarray(result).save('result.png')
    

    你会得到这个:

    关键字: numpy、Python、图像、图像处理、合成、混合、混合模式、变亮、变亮、Photoshop、等效、变暗、叠加。

    【讨论】:

    • 非常感谢您的帮助,您说得对,我的代码过于复杂了。上面的代码完美运行
    • 很高兴它对你有用。祝你的项目好运!
    • 当我使用新图像尝试此代码时,我收到错误消息“操作数无法与形状一起广播 (1814,2085) (1863,1936)。我想知道如何解决这个问题使形状大小相同,以便它们可以融合在一起?再次感谢迄今为止的所有帮助
    • 哦,我并不担心调整大小,因为我认为您已经从代码中解决了这个问题。执行Image.open('a.png').convert('L') 部分,然后精确调整您之前调整大小的方式,然后执行np.array(resizedImage) 并像我一样继续。
    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多