B. Dreamer
B. Dreamer

Reputation: 39

overlay an image and show lighter pixel at each pixel location

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.

Upvotes: 1

Views: 747

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207530

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.

Upvotes: 1

Related Questions