Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created September 23, 2017 05:28
Show Gist options
  • Save garybradski/fabe4ce7ed5c042988b748780393370c to your computer and use it in GitHub Desktop.
Save garybradski/fabe4ce7ed5c042988b748780393370c to your computer and use it in GitHub Desktop.
Blend one image with another using an alpha mask in python
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)
@Soumi7
Copy link

Soumi7 commented Feb 25, 2021

I get the same error as @Roopashaily . Has anyone resolved this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment