使用PIL库将一张小图贴到大图的指定位置
生活随笔
收集整理的這篇文章主要介紹了
使用PIL库将一张小图贴到大图的指定位置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# FileName : copyHandsToMask
# Author : Donghao
# CreateTime : 2021-10-24 17:25
# ModifyTime : 2021-10-24 17:25
# Description : copy mask of hands to body maskimport os
import cv2
import glob
import tqdm
import numpy as np
from PIL import Image# #手部標注存儲路徑
# hands_mask_dir = './15-4-tmp8/tmp8_hands_resieze4_labels/'
# #全身標注存儲路徑
# body_mask_dir = './15-4-tmp8/tmp8_labels/'
# #重新合成文件存儲路徑
# result_dir = './15-4-tmp8/annotations/'
#手部標注存儲路徑
hands_mask_dir = './14_4-real/tmp4/tmp4_hands_resieze4_labels/'
#全身標注存儲路徑
body_mask_dir = './14_4-real/tmp4/tmp4_labels/'
#重新合成文件存儲路徑
result_dir = './14_4-real/tmp4/annotations/'
if os.path.exists(result_dir) is False:os.makedirs(result_dir)def Picture_Synthesis(mother_img,son_img,save_img,factor = 1,coordinate=None):""":param mother_img: 母圖路徑:param son_img: 子圖路徑:param save_img: 保存圖片名:param coordinate: 子圖在母圖的坐標:param factor: 子圖縮放因子,默認為1,即不進行縮放:return:"""#將圖片賦值,方便后面的代碼調用M_Img = Image.open(mother_img)S_Img = Image.open(son_img)#給圖片指定色彩顯示格式M_Img = M_Img.convert("RGBA") # CMYK/RGBA 轉換顏色格式(CMYK用于打印機的色彩,RGBA用于顯示器的色彩)# 獲取圖片的尺寸M_Img_w, M_Img_h = M_Img.size # 獲取被放圖片的大小(母圖)# print("母圖尺寸:",M_Img.size)S_Img_w, S_Img_h = S_Img.size # 獲取小圖的大小(子圖)# print("子圖尺寸:",S_Img.size)# 対子圖進行縮放size_w = int(S_Img_w / factor)size_h = int(S_Img_h / factor)if S_Img_w > size_w:S_Img_w = size_wif S_Img_h > size_h:S_Img_h = size_h# # 重新設置子圖的尺寸icon = S_Img.resize((S_Img_w, S_Img_h), Image.ANTIALIAS)w = int((M_Img_w - S_Img_w) / 2)h = int((M_Img_h - S_Img_h) / 2)# 第二個參數:# Image.NEAREST :低質量# Image.BILINEAR:雙線性# Image.BICUBIC :三次樣條插值# Image.ANTIALIAS:高質量try:if coordinate==None or coordinate=="":# print("未指定坐標,劇中粘貼")# 粘貼子圖到母圖的指定坐標(當前居中)coordinate=(w, h)M_Img.paste(icon, coordinate, mask=None)else:# print("已經指定坐標")# 粘貼子圖到母圖的指定坐標(當前居中)M_Img.paste(icon, coordinate, mask=None)except BaseException as e:print("坐標指定出錯 ")with open('./errorlog.txt', 'a') as f:f.write(str(e) + '\n\n')# 保存圖片M_Img.save(save_img)if __name__ == '__main__':# root_path = '.'# dirlist = os.listdir(root_path)# for dir in tqdm(dirlist):# child_path = os.path.join(root_path, dir)# childlist = os.listdir(child_path)## for child in childlist:# work_path = os.path.join(child_path, child)# work_dir_list = os.listdir(work_path)# #手部標注存儲路徑# hands_mask_dir = './14_2-real/14-2-temp2/tmp2_hands_labels/'# #全身標注存儲路徑# body_mask_dir = './14_2-real/14-2-temp2/tmp2_labels/'# #重新合成文件存儲路徑# result_dir = './14_2-real/14-2-temp2/annotations/'# if os.path.exists(result_dir) is False:# os.makedirs(result_dir)hands_mask_path_list = glob.glob(hands_mask_dir + '*pseudo.jpg')body_mask_path_list = glob.glob(body_mask_dir + '*pseudo.png')# print(type(hands_mask_path_list), len(hands_mask_path_list), hands_mask_path_list[0])# 打開包含手坐標信息的txt文件with open('./hands_boxes.txt', 'r') as rf:hands_boxes_file = rf.readlines()# print(type(hands_boxes_file), hands_boxes_file[4],hands_boxes_file[1])for hands_mask_path in tqdm.tqdm(hands_mask_path_list):# 判斷是否為全黑圖片hands_mask = Image.open(hands_mask_path)r, g, b = hands_mask.getextrema()if r[1] == 0 and g[1] == 0 and b[1] == 0:continuehands_mask_name = os.path.basename(hands_mask_path)hand_number = hands_mask_name[-12:-11]origin_image_name = hands_mask_name[:-13] + '.png'body_mask_name = hands_mask_name[:-13] + '_pseudo.png'body_mask_path = body_mask_dir + body_mask_name# print(hand_number)# print(origin_image_name)# print(body_mask_name)# print(body_mask_path)if os.path.isfile(body_mask_path) is False:print(f"Body mask image not found: ")print(f"hands_mask_path: {hands_mask_path}")print(f"body_mask_path: {body_mask_path}")with open('./errorlog.txt', 'a') as f:f.write(f"Body mask image not found: \nhands_mask_path: {hands_mask_path}\nbody_mask_path: {body_mask_path}\n\n")continuesave_path = os.path.join(result_dir, origin_image_name)if os.path.isfile(save_path):body_mask_path = save_pathindex = hands_boxes_file.index(origin_image_name + '\n')start_index = index + (int(hand_number) - 1) * 5 + 2lx = int(float(hands_boxes_file[start_index]))ly = int(float(hands_boxes_file[start_index + 1]))rx = int(float(hands_boxes_file[start_index + 2]))ry = int(float(hands_boxes_file[start_index + 3]))Picture_Synthesis(mother_img = body_mask_path,son_img = hands_mask_path,save_img = save_path,factor = 4,coordinate=(ly, lx))for body_mask_path in tqdm.tqdm(body_mask_path_list):body_mask_name = os.path.basename(body_mask_path)body_mask_name = body_mask_name[: -11] + '.png'save_path = os.path.join(result_dir, body_mask_name)if os.path.isfile(save_path) is False:img = Image.open(body_mask_path)img.save(save_path)
總結
以上是生活随笔為你收集整理的使用PIL库将一张小图贴到大图的指定位置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cityscapse 数据集使用 + 训
- 下一篇: 借助numpy.rot90实现图片顺时针