pythonopencvimage-processingalphablendingalpha-transparency

Using openCV to overlay transparent image onto another image


How can I overlay a transparent PNG onto another image without losing it's transparency using openCV in python?

import cv2

background = cv2.imread('field.jpg')
overlay = cv2.imread('dice.png')

# Help please

cv2.imwrite('combined.png', background)

Desired output: enter image description here

Sources:

Background Image

Overlay


Solution

  • import cv2
    
    background = cv2.imread('field.jpg')
    overlay = cv2.imread('dice.png')
    
    added_image = cv2.addWeighted(background,0.4,overlay,0.1,0)
    
    cv2.imwrite('combined.png', added_image)
    

    added_image