Created
May 16, 2020 16:24
-
-
Save staybuzz/922d158599a88a6050bacd79dcf2bd27 to your computer and use it in GitHub Desktop.
Revisions
-
staybuzz created this gist
May 16, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ import cv2 import numpy as np from PIL import Image, ImageDraw # ref: https://note.com/npaka/n/nddb33be1b782 # 画像のオーバーレイ def overlayImage(src, overlay, location): overlay_height, overlay_width = overlay.shape[:2] # 背景をPIL形式に変換 src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) pil_src = Image.fromarray(src) pil_src = pil_src.convert('RGBA') # オーバーレイをPIL形式に変換 overlay = cv2.cvtColor(overlay, cv2.COLOR_BGRA2RGBA) pil_overlay = Image.fromarray(overlay) pil_overlay = pil_overlay.convert('RGBA') # 画像を合成 pil_tmp = Image.new('RGBA', pil_src.size, (0, 0, 0, 0)) pil_tmp.paste(pil_overlay, location, pil_overlay) result_image = Image.alpha_composite(pil_src, pil_tmp) # OpenCV形式に変換 return cv2.cvtColor(np.asarray(result_image), cv2.COLOR_RGBA2BGRA) overlay_img = cv2.imread('youngman_33.png', cv2.IMREAD_COLOR) overlay_img = cv2.resize(overlay_img, (110, 110)) a = overlayImage(l_p1_img, overlay_img, (440,50)) b = overlayImage(l_p2_img, overlay_img, (550,170)) c = overlayImage(r_p1_img, overlay_img, (330,50)) d = overlayImage(r_p2_img, overlay_img, (290,190)) cv2.imwrite('a.png', a) cv2.imwrite('b.png', b) cv2.imwrite('c.png', c) cv2.imwrite('d.png', d) mask_p1_img = cv2.hconcat([a, c]) mask_p2_img = cv2.hconcat([b, d]) mask_im1 = Image.fromarray(cv2.cvtColor(mask_p1_img, cv2.COLOR_BGR2RGB)) mask_im2 = Image.fromarray(cv2.cvtColor(mask_p2_img, cv2.COLOR_BGR2RGB)) mask_im1.save('test_mask.gif', save_all=True, append_images=[mask_im2], optimize=False, duration=700, loop=0)