Python+OpenCV How to realize multiple sheets png Transparent image superimposed on jpg On the image ?? Just like this picture Multiple sheets png Superimposed on jpg On the picture
Try this version :
import cv2import numpy as np def add_alpha_channel(img): """ by jpg Image add alpha passageway """ b_channel, g_channel, r_channel = cv2.split(img) # be stripped jpg Image channel alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255 # establish Alpha passageway img_new = cv2.merge((b_channel, g_channel, r_channel, alpha_channel)) # Fusion channel return img_new def merge_img(jpg_img, png_img, png2_img,y1, y2, x1, x2, y3, y4, x3, x4): """ take png Transparent images and jpg Image overlay y1,y2,x1,x2 Is the superposition position coordinate value """ # Judge jpg Whether the image is already 4 passageway if jpg_img.shape[2] == 3: jpg_img = add_alpha_channel(jpg_img) ''' When superimposing images , It may be because the stacking position is not set properly , Lead to png The boundary of the image is beyond the background jpg Images , And the program reports an error Here set a series of stacking position limits , You can meet png Image out of range jpg Image range , It can still stack normally ''' yy1 = 0 yy2 = png_img.shape[0] xx1 = 0 xx2 = png_img.shape[1] if x1 < 0: xx1 = -x1 x1 = 0 if y1 < 0: yy1 = - y1 y1 = 0 if x2 > jpg_img.shape[1]: xx2 = png_img.shape[1] - (x2 - jpg_img.shape[1]) x2 = jpg_img.shape[1] if y2 > jpg_img.shape[0]: yy2 = png_img.shape[0] - (y2 - jpg_img.shape[0]) y2 = jpg_img.shape[0] # Get the image to be overwritten alpha value , Divide the pixel value by 255, Keep the value at 0-1 Between alpha_png = png_img[yy1:yy2,xx1:xx2,3] / 255.0 alpha_jpg = 1 - alpha_png png2_yy1 = 0 png2_yy2 = png_img.shape[0] png2_xx1 = 0 png2_xx2 = png_img.shape[1] if x3 < 0: png2_xx1 = -x3 x3 = 0 if y3 < 0: png2_yy1 = - y3 y3 = 0 if x4 > jpg_img.shape[1]: png2_xx2 = png_img.shape[1] - (x4 - jpg_img.shape[1]) x4 = jpg_img.shape[1] if y4 > jpg_img.shape[0]: png2_yy2 = png_img.shape[0] - (y4 - jpg_img.shape[0]) y4 = jpg_img.shape[0] # Get the image to be overwritten alpha value , Divide the pixel value by 255, Keep the value at 0-1 Between png2_alpha_png = png2_img[png2_yy1:png2_yy2,png2_xx1:png2_xx2,3] / 255.0 png2_alpha_jpg = 1 - png2_alpha_png # Start stacking for c in range(0,3): jpg1_img[y1:y2, x1:x2, c] = ((alpha_jpg*jpg_img[y1:y2,x1:x2,c]) + (alpha_png*png_img[yy1:yy2,xx1:xx2,c])) jpg_img[y3:y4, x3:x4, c] = ((png2_alpha_jpg*jpg1_img[y1:y2,x1:x2,c]) + (png2_alpha_png*png2_img[yy1:yy2,xx1:xx2,c])) return jpg_img if __name__ == '__main__': # Define image path img_jpg_path = '3.jpg' # Readers can modify the file path by themselves img_png_path = '1.png' # Readers can modify the file path by themselves img2_png_path = '2.png' # Read images img_jpg = cv2.imread(img_jpg_path, cv2.IMREAD_UNCHANGED) img_png = cv2.imread(img_png_path, cv2.IMREAD_UNCHANGED) img2_png = cv2.imread(img2_png_path, cv2.IMREAD_UNCHANGED) # Set the stacking position coordinates # Figure 1 x1 = 560 y1 = 180 x2 = x1 + img_png.shape[1] y2 = y1 + img_png.shape[0] # Figure 2 x3 = 100 y3 = 100 x4 = x3 + img2_png.shape[1] y4 = y3 + img2_png.shape[0] # Start stacking res_img = merge_img(jpg_img, png_img, png2_img,y1, y2, x1, x2,y3, y4, x3, x4) # Display the result image cv2.imshow('result', res_img) # Save the result image , Readers can modify the file path by themselves cv2.imwrite('imgs/res.jpg', res_img) # Define how the program exits : Click the window displaying the image with the mouse , Press ESC Key to exit the program if cv2.waitKey(0) & 0xFF == 27: cv2.destroyAllWindows()
If there is no problem, please click "adopt"
Introduction The new year ush
In the usual games , We are Ex