Home > Net >  Python OpenCV Duplicate a transparent shape in the same image
Python OpenCV Duplicate a transparent shape in the same image

Time:10-03

I have an image of a circle, refer to the image attached below. I already retrieved the transparent circle and want to paste that circle back to the image to make some overlapped circles.

Below is my code but it led to the problem A, it's like a (transparent) hole in the image. I need to have circles on normal white background.

height, width, channels = circle.shape
original_image[60:60 height, 40:40 width] = circle

I used cv2.addWeighted but got blending issue, I need clear circles

circle = cv2.addWeighted(original_image[60:60 height, 40:40 width],0.5,circle,0.5,0)
original_image[60:60 rows, 40:40 cols] = circle

Need to implement the flow to B

CodePudding user response:

If you already have a transparent black circle, then in Python/OpenCV here is one way to do that.

 - Read the transparent image unchanged
 - Extract the bgr channels and the alpha channel
 - Create a colored image of the background color and size desired
 - Create similar sized white and black images
 - Initialize a copy of the background color image for the output
 - Define a list offset coordinates in the larger image
 - Loop for over the list of offsets and do the following
 - Insert the bgr image into a copy of the white image as the base image
 - Insert the alpha channel into a copy of the black image for a mask
 - composite the initialized output and base images using the mask image
 - When finished with the loop, save the result

Input (transparent):

enter image description here

import cv2
import numpy as np

# load image with transparency
img = cv2.imread('black_circle_transp.png', cv2.IMREAD_UNCHANGED)
height, width = img.shape[:2]
print(img.shape)

# extract the bgr channels and the alpha channel
bgr = img[:,:,0:3]
aa = img[:,:,3]
aa = cv2.merge([aa,aa,aa])

# create whatever color background you want, in this case white
background=np.full((500,500,3), (255,255,255), dtype=np.float64)

# create white image of the size you want
white=np.full((500,500,3), (255,255,255), dtype=np.float64)

# create black image of the size you want
black=np.zeros((500,500,3), dtype=np.float64)

# initialize output
result = background.copy()

# define top left corner x,y locations for circle offsets
xy_offsets = [(100,100), (150,150), (200,200)]

# insert bgr and alpha into white and black images respectively of desired output size and composite
for offset in xy_offsets:
    xoff = offset[0]
    yoff = offset[1]
    base = white.copy()
    base[yoff:height yoff, xoff:width xoff] = bgr
    mask = black.copy()
    mask[yoff:height yoff, xoff:width xoff] = aa
    result = (result * (255-mask)   base * mask)/255
    result = result.clip(0,255).astype(np.uint8)

# save resulting masked image
cv2.imwrite('black_circle_composite.png', result)

# display result, though it won't show transparency
cv2.imshow("image", img)
cv2.imshow("aa", aa)
cv2.imshow("bgr", bgr)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here

  • Related