Skip to content
Advertisement

How to overlay two non-transparent images in Pillow

I’m trying to make a simple image manipulation function that applies a soviet flag filter to a profile picture. Both pictures are non-transparent. I have some code

from PIL import Image

app = Flask(__name__)

img = Image.open("soviet.jpg")
back = Image.open("profile.jpg")

back.paste(img, (0, 0))
back.show()

That would work if either of the images were transparent. However, because my images are not transparent, it just shows the background. Also, the pictures have different resolutions, so it just shows the top left of the background. Is there a way to center & set the opacity of the profile picture, and then overlay it?

Advertisement

Answer

you may need to resize the images to match each other with the following:

back = back.resize(img.size)

then try using the blend() function:

blended_image = Image.blend(img, back, 0.5)
Advertisement