import os
import time
from os import listdir
from PIL import Image
from loguru import logger
from PIL import Image
def image_synthesis(mother_img,son_img,save_img,size_data,coefficient=2.5,coordinate=None):
M_Img=Image.open(mother_img)
S_Img=Image.open(son_img)
M_Img=M_Img.convert("RGBA")
M_Img_w,M_Img_h=M_Img.size
logger.info(f"母图尺寸:{M_Img.size}")
S_Img_w,S_Img_h=S_Img.size
logger.info(f"子图尺寸:{S_Img.size}")
son_resize_h=size_data/coefficient
factor=son_resize_h/S_Img_h if son_resize_h>S_Img_h else S_Img_h/son_resize_h#子图缩小的倍数1代表不变,2就代表原来的一半
logger.info(f"子图重置比例: {factor}")
size_w=int(S_Img_w/factor)
size_h=int(S_Img_h/factor)
#防止子图尺寸大于母图
if S_Img_w>size_w:
logger.info(f"防止子图尺寸大于母图")
S_Img_w=size_w
if S_Img_h>size_h:
logger.info(f"防止子图尺寸大于母图")
S_Img_h=size_h
#重新设置子图的尺寸
icon=S_Img.resize((S_Img_w,S_Img_h),Image.ANTIALIAS)
logger.info(f"重置后子图尺寸:{(S_Img_w,S_Img_h)}")
try:
if not coordinate or coordinate=="":
w=int((M_Img_w-S_Img_w)/2)
h=int((M_Img_h-S_Img_h))
coordinate=(w,h)
#粘贴子图到母图的指定坐标(当前水平居中垂直靠下)
M_Img.paste(icon,coordinate,mask=None)
else:
logger.info("已经指定坐标")
#粘贴子图到母图的指定坐标(指定坐标)
M_Img.paste(icon,coordinate,mask=None)
except:
logger.info("坐标指定出错 ")
M_Img.save(save_img)
return save_img
def image_stitching(origin_img_path, result_img_path, output_img_path, size_data):
origin_data=Image.open(origin_img_path)
result_data=Image.open(result_img_path)
M_Img_w, M_Img_h=origin_data.size
logger.info(f"待拼接图片的原尺寸: {(M_Img_w,M_Img_h)}")
factor=M_Img_h/size_data if size_data>M_Img_h else size_data/M_Img_h#子图缩小的倍数1代表不变,2就代表原来的一半
size_w=int(M_Img_w/factor)
logger.info(f"待拼接图片重置尺寸: {(size_w,size_data)}")
origin_img=origin_data.resize((size_w,size_data),Image.BILINEAR)
result_img=result_data.resize((size_w,size_data),Image.BILINEAR)
image_list=[origin_img,result_img]
#单幅图像尺寸
width,height=image_list[0].size
logger.info(f"--- width={width},heigh={height}")
#创建空白长图
result=Image.new(image_list[0].mode,(width*len(image_list),height))
#拼接图片
for i, im in enumerate(image_list):
result.paste(im,box=(i*width,0))
result.save(output_img_path)
return stitching_img_path
if __name__=='__main__':
root_path='C:/Users/xpp/Desktop/Lena'
size_data=1280#重制尺寸值
origin_img_path=os.path.join(root_path,'1.png')
result_img_path=os.path.join(root_path,'2.png')
face_img_path=os.path.join(root_path,'3.png')
stitching_img_path=os.path.join(root_path,'4.png')
#两图左右拼接
last_img_path=image_stitching(origin_img_path,result_img_path,stitching_img_path,size_data)
logger.info(f"左右拼接完成 ---")
#覆盖小图片到拼接图居中靠下
synthesis_img_path=os.path.join(root_path,'synthesis_.png')
res=image_synthesis(last_img_path,face_img_path,synthesis_img_path,size_data)
logger.info(f"--- end --- res = {res}")
2021-12-10 21:57:19.573 | INFO | __main__:image_stitching:49 - 待拼接图片的原尺寸: (460, 460) 2021-12-10 21:57:19.575 | INFO | __main__:image_stitching:52 - 待拼接图片重置尺寸: (1280, 1280) 2021-12-10 21:57:19.654 | INFO | __main__:image_stitching:58 - --- width=1280,heigh=1280 2021-12-10 21:57:20.915 | INFO | __main__:<module>:75 - 左右拼接完成 --- 2021-12-10 21:57:21.070 | INFO | __main__:image_synthesis:12 - 母图尺寸:(2560, 1280) 2021-12-10 21:57:21.071 | INFO | __main__:image_synthesis:14 - 子图尺寸:(460, 460) 2021-12-10 21:57:21.073 | INFO | __main__:image_synthesis:17 - 子图重置比例: 1.1130434782608696 2021-12-10 21:57:21.075 | INFO | __main__:image_synthesis:22 - 防止子图尺寸大于母图 2021-12-10 21:57:21.076 | INFO | __main__:image_synthesis:25 - 防止子图尺寸大于母图 2021-12-10 21:57:21.102 | INFO | __main__:image_synthesis:29 - 重置后子图尺寸:(413, 413) 2021-12-10 21:57:22.817 | INFO | __main__:<module>:79 - --- end --- res = C:/Users/xpp/Desktop/Lena\synthesis_.png 算法:图像覆盖堆叠是包括图像读取,图片尺寸读取,重置图片大小,图片等比缩放,图片拼接,图片覆盖与堆叠(子母图)在内。 链接:https://www.cnpython.com/tags/290753
本文分享自 图像处理与模式识别研究所 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!