Python 覆盖图像并在每个像素位置显示较亮的像素

Python 覆盖图像并在每个像素位置显示较亮的像素,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我有两个黑白图像,我想与最终图像合并,在两个图像中的每个像素位置显示较亮/白色像素。我尝试了以下代码,但没有成功 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

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

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()

我收到的错误消息是无法处理此数据类型。任何人能提供的任何帮助或建议都是非常好的

我认为你把事情复杂化了。您只需读入两幅图像并将其设置为灰度
numpy
阵列,然后在每个位置选择两个像素中较亮的一个

从这两张图片开始:

您将获得以下信息:


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

非常感谢您的帮助,您是对的,我的代码过于复杂了。上面的代码工作得非常好,很高兴它为您工作。祝你的项目好运!当我用一个新图像尝试这段代码时,我得到错误消息“操作数不能与形状一起广播(18142085)(18631936)。我想知道如何解决这个问题,使形状大小相同,以便它们可以混合在一起?再次感谢到目前为止的所有帮助。哦,我不担心调整大小,因为我认为您已经从代码中解决了这个问题。执行
Image.open('a.png')。convert('L'))
part,然后精确调整之前调整大小的方式,然后执行
np.array(resizedImage)
并像我一样继续。
#!/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')