from PIL import Image as image foreground = image.open("donkey.png") background = image.open( "back.jpg" ) alpha = image.open( "donkeyAlpha.png" ) outImage = image.composite( foreground, background, alpha ) outImage.save( "./pilblend.png" )
import cv2 foreground = cv2.imread("donkey.png") background = cv2.imread("back.jpg") alpha = cv2.imread("donkeyAlpha.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 foreground = cv2.multiply(alpha, foreground) background = cv2.multiply(1.0 - alpha, background) outImage = cv2.add(foreground, background) cv2.imwrite( "./cv2blend.png" ,outImage )