portfan.blogg.se

Overlay images with transparency python
Overlay images with transparency python





  1. #Overlay images with transparency python how to#
  2. #Overlay images with transparency python code#

# combine the background with the overlay image weighted by alphaĬomposite = background_subsection * (1 - alpha_mask) + overlay_colors * alpha_mask # could be used to place the overlay at any position on the background.īackground_subsection = background # NOTE: For simplicity, the overlay is applied to the top-left corner of the background(0,0). # The background image is larger than the overlay so we'll take a subsection of the background that matches the # for each color so there is a 1:1 alpha channel for each color channelĪlpha_mask = np.dstack((alpha_channel, alpha_channel, alpha_channel)) # We will construct an alpha_mask that has the same shape as the overlay_colors by duplicate the alpha channel

overlay images with transparency python

# - alpha_channel shape:(width, height, 1) 1 single alpha value for each pixel # - overlay_colors shape:(width, height, 3) 3 color values for each pixel, (red, green, blue) However, the shapes currently looks like this: # To take advantage of the speed of numpy and apply transformations to the entire image with a single operation # separate the alpha channel from the color channelsĪlpha_channel = overlay / 255 # convert from 0-255 to 0.0-1.0 Note: This was my first real foray into numpy so there may be better/faster methods than what I've come up with. We can make the algorithm significantly faster by using numpy's vector functions. The above solution is way too slow for that. I stumbled across this question while trying to add a png overlay to a live video feed. # combine the background color and the overlay color weighted by alphaĬomposite_color = background_color * (1 - overlay_alpha) + overlay_color * overlay_alpha

overlay images with transparency python

# get the color from the background image Overlay_alpha = overlay / 255 # 4th element is the alpha channel, convert from 0-255 to 0.0-1.0 Overlay_color = overlay # first three elements are color (RGB) Overlay = cv2.imread('dice.png', cv2.IMREAD_UNCHANGED) # IMREAD_UNCHANGED => open image with the alpha channel

#Overlay images with transparency python how to#

This isn't very efficient, but it does help to understand how to work with png's alpha layer. If performance isn't a concern then you can iterate over each pixel of the overlay and apply it to the background.

#Overlay images with transparency python code#

This code will mutate background so create a copy if you wish to preserve the original background image. Np.ones((overlay.shape, overlay.shape, 1), dtype = overlay.dtype) * 255īackground = (1.0 - mask) * background + mask * overlay_image If x >= background_width or y >= background_height: import cv2ĭef overlay_transparent(background, overlay, x, y): The following code will use the alpha channels of the overlay image to correctly blend it into the background image, use x and y to set the top-left corner of the overlay image. # set adjusted alpha and denormalize back to 0-255īackground = (1 - (1 - alpha_foreground) * (1 - alpha_background)) * 255Ĭv2.imshow("Composited image", background)

overlay images with transparency python

# normalize alpha channels from 0-255 to 0-1Īlpha_background = background / 255.0Īlpha_foreground = foreground / 255.0īackground = alpha_foreground * foreground + \Īlpha_background * background * (1 - alpha_foreground) I am far from an expert with OpenCV, but after some experimentation this is the most efficient way I have found to accomplish the task: import cv2īackground = cv2.imread("background.png", cv2.IMREAD_UNCHANGED)įoreground = cv2.imread("overlay.png", cv2.IMREAD_UNCHANGED) What you are looking for is "over" compositing, and the algorithm for this can be found on Wikipedia: The correct answer to this was far too hard to come by, so I'm posting this answer even though the question is really old.







Overlay images with transparency python