Skip to content Skip to sidebar Skip to footer

Valueerror: Images Do Not Match When Blending Pictures In Pil

I have been messing around in python to see if I could 'mix' two pictures together. What I mean by that is so that the image is transparent and you can see two pictures together. I

Solution 1:

There are several things going on here:

  • Your input images are both JPEG which doesn't support transparency, so you can only get a fixed blending throughout your image. I mean you can't see one image at one point and the other image at another. You will only see the same proportion of each image at each point. Is that what you want?

For example, if I take Paddington and Buckingham Palace and take 50% of each:

enter image description here

enter image description here

I get this:

enter image description here

If that's what you want, you need to resize the images to a common size and change this line:

bg = Image.blend(im1, im2, 0)

to

bg = Image.blend(im1, im2, 0.5)          # blend half and half
  • If you want to paste something with transparency, so it only shows up in certain places, you need to load the overlay from a GIF or PNG with transparency and use:

    background.paste(overlay, box=None, mask=overlay)

Then you can do this - note you can see different amounts of the two images at each point:

enter image description here

So, as a concrete example of overlaying a transparent image onto an opaque background, and starting with Paddington (400x400) and this star (500x500):

enter image description here

enter image description here

#!/usr/bin/env python3from PIL import Image

# Open background and foreground and ensure they are RGB (not palette)
bg = Image.open('paddington.png').convert('RGB')
fg = Image.open('star.png').convert('RGBA')

# Resize foreground down from 500x500 to 100x100
fg_resized = fg.resize((100,100))

# Overlay foreground onto background at top right corner, using transparency of foreground as mask
bg.paste(fg_resized,box=(300,0),mask=fg_resized)

# Save result
bg.save('result.png')

enter image description here

If you want to grab an image from a website, use this:

from PIL import Image 
import requests 
from io import BytesIO                                                                      

# Grab the star image from this answer
response = requests.get('https://i.stack.imgur.com/wKQCT.png')                              

# Make it into a PIL image
img = Image.open(BytesIO(response.content))  

Solution 2:

As an alternative, you could try with OpenCV (depending on your desired output)

this is the result of the code

import cv2
 
# Read the images
     foreground = cv2.imread("puppets.png")
     background = cv2.imread("ocean.png")
     alpha = cv2.imread("puppets_alpha.png")
 
# Convert uint8 to float
     foreground = foreground.astype(float)
     background = background.astype(float)
 
# Normalize the alpha mask to keep intensity between 0 and 1
     alpha = alpha.astype(float)/255# Multiply the foreground with the alpha matte
     foreground = cv2.multiply(alpha, foreground)
 
# Multiply the background with ( 1 - alpha )
     background = cv2.multiply(1.0 - alpha, background)
 
# Add the masked foreground and background.
     outImage = cv2.add(foreground, background)
 
# Display image
     cv2.imshow("outImg", outImage/255)
     cv2.waitKey(0)

Post a Comment for "Valueerror: Images Do Not Match When Blending Pictures In Pil"