overlay an image and show lighter pixel at each pi

2019-08-02 02:30发布

I have two black and white images that I would like to merge with the final image showing the lighter/ white pixel at each pixel location in both images. I tried the following code but it did not work.

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

The error message I get is cannot handle this data type. Any help or advice anyone could give would be great.

1条回答
叼着烟拽天下
2楼-- · 2019-08-02 03:12

I think you are over-complicating things. You just need to read in both images and make them greyscale numpy arrays, then choose the lighter of the two pixels at each location.

So starting with these two images:

enter image description here enter image description here

#!/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')

You will get this:

enter image description here

Keywords: numpy, Python, image, image processing, compose, blend, blend mode, lighten, lighter, Photoshop, equivalent, darken, overlay.

查看更多
登录 后发表回答