# 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库将一张小图贴到大图的指定位置相关推荐

  1. pip 安装库时磁盘空间不足怎么办?如何从指定位置安装相应的包

    pip 安装库是磁盘空间不足怎么办 磁盘空间不足,可以考虑释放空间,或增大空间,释放空间指的是默认情况下,将主文件夹下面的没用的文件删除一些,同时可以将缓存文件清空一些,linux的是~/.cache ...

  2. python image模块安装_python之PIL库(Image模块)

    PIL(Python Image Library)是python的第三方图像处理库,PIL的功能非常的强大,几乎被认定是Python的官方图像处理库了. 由于PIL仅支持到python2.7于是一群志 ...

  3. PIL库自我学习总结及应用(美白,磨皮,搞笑图片处理)

    Hello!今天我们来学习一下这个神奇的图片处理的第三方函数库--PIL库 (本blog部分图片及代码来自网络) 这是一个支持图像存储.显示和处理的函数库,它能够处理几乎所有图像格式,可以完成对图像的 ...

  4. image pil 图像保存_如何利用python中的PIL库做图像处理?

    自从这个世界上出现了Python编程,一切都好像有了新的思路与进展,比如人工智能,还有我们常用的PS,你可知道Python也可以做图像处理,用的就是PIL库,还没有用过的,还没有发现的,还没有实现过的 ...

  5. Python Pillow(PIL)库的用法介绍(二)

    Python Pillow(PIL)库的用法介绍(二) 在上一篇文章中介绍了Pillow库的一些基本用法,参考:https://blog.csdn.net/weixin_43790276/articl ...

  6. python拼图_用python的PIL库轻松拼接一百张照片

    多图预警,请在wifi下观看. 和大家分享下之前用python的第三方库PIL库进行多图拼接制作的一些漂亮的照片墙成果图. 一.微信好友头像 我不会Photoshop(PS),所有知道PIL库可以处理 ...

  7. 【1.6万字】连续抓屏保存为Gif动图 【keyboard库、PIL库、imageio库和pygifsicle库 探索】

    目录 一.抓屏保存为Gif 二.keyboard库 三.PIL 库 3.0 安装Pillow 3.1 打开本地图片 3.2 创建一张新图片 3.3 Image模块的常用属性 3.4 图片的模式和模式转 ...

  8. 新发的日常小实验——使用python的PIL库批量修改图片尺寸,确保宽和高是4的倍数(Unity、PIL、Pillow、压缩)

    文章目录 一.问题:图片导入Unity被调整成2的N次方 二.设置不转换成2的N次方 三.使用ETC压缩格式,图片宽高必须是4的倍数 四.使用python的PIL库批量修改图片尺寸,确保宽和高是4的倍 ...

  9. PIL库中Image类thumbnail方法和resize方法区别

    from PIL import Image   im=Image.open("C:\\Users\\kethur\\Desktop\\a.jpg") x,y=im.size pri ...

最新文章

  1. Centos 开机无法输入密码的问题
  2. [bbk5307]第76集 第9章 -数据库性能维护 03
  3. 树莓派UART串口编程--使用wiringPi库-C开发【2-修改驱动】
  4. 流量复制_快速体验之《gor+diffy实现线上流量复制到测试环境》
  5. linux下的dns服务器
  6. 【BZOJ3930】[CQOI2015]选数 莫比乌斯反演
  7. C++库研究笔记——Linux下是否需要使用memory pool?
  8. discuz mysql 编码_Discuz!X 下如何使用 Tools 来转换数据库编码
  9. 论文笔记_S2D.42_2018-CRV_为经典图像处理辩护:在CPU上的快速深度补全
  10. mysql5.7.18压缩包下载_MySQL5.6.30 升级到MySQL5.7.18
  11. java虚拟机内存溢出的三个原因_java虚拟机学习(三) 内存溢出异常
  12. Spring Data ElasticSearch - 分布式搜索和数据分析引擎 相关操作实战流程
  13. 微信授权登录(更新。。。)
  14. 广东神州行如何拨打长长途才实惠?
  15. 计算机一级表格技巧,计算机一级考试MS Office应试技巧指导
  16. 学习笔记(十)在网页中添加矢量图形
  17. 记录ssl证书过期,更新证书的过程
  18. 《模型轻量化-剪枝蒸馏量化系列》YOLOv5无损剪枝(附源码)
  19. Java单元测试实践-09.Mockito的Stub参数条件
  20. 一个正确的编程学习方法

热门文章

  1. 【实用】用QuickViewer收集数据
  2. Smart Form不弹出假脱机设置界面直接打印预览
  3. 我的爸爸是xiang目经理.....
  4. ABAP-SQL基础知识
  5. SAP-HR三大结构
  6. excel中如何筛选重复数据
  7. 跨工厂物料状态/特定工厂的物料状态
  8. SM12表条目冻结说明
  9. linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法
  10. 安卓虚拟机_安卓虚拟机(*New*)v1.1.31去广告/去推荐/Mod/精简/VIP版