程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How does python+opencv stack multiple PNG transparent images on jpg images??

編輯:Python

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




Take the answer :

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"



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved