-
-
Save tiivik/9187368f6ce51c22467a98d75b802bcc to your computer and use it in GitHub Desktop.
Blend one image with another using an alpha mask in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment