Python图像处理之图片拼接和堆叠案例教程

作者:Saggitarxm 时间:2022-04-08 13:36:04 

业务说明:

此示例脚本作用,包含方法和逻辑:图像读取,图片尺寸读取,重置图片大小,图片等比缩放,图片拼接,图片覆盖与堆叠(子母图)

图片展示:

单张素材:

origin_image.jpg

Python图像处理之图片拼接和堆叠案例教程

result_image.jpg

Python图像处理之图片拼接和堆叠案例教程

face_image.jpg

Python图像处理之图片拼接和堆叠案例教程

 拼接结果示例图:

Python图像处理之图片拼接和堆叠案例教程

拼接和堆叠完成后示例:

Python图像处理之图片拼接和堆叠案例教程

拼接和堆叠完成后示例2:

Python图像处理之图片拼接和堆叠案例教程

拼接和堆叠完成后示例3: 

Python图像处理之图片拼接和堆叠案例教程

代码示例:


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):
   """
   mother_img="C:/Users/Administrator/Desktop/QRCode/b.jpg",
   son_img="C:/Users/Administrator/Desktop/QRCode/y.png",
   save_img="C:/Users/Administrator/Desktop/QRCode/newimg.png",
   coordinate=None#如果为None表示直接将子图在母图中居中也可以直接赋值坐标
   # coordinate=(50,50)
   :param mother_img: 母图
   :param son_img: 子图
   :param save_img: 保存图片名
   :param size_data: 母图的高
   :param coefficient: 子图相对于母图高度压缩系数
   :param coordinate: 子图在母图的坐标 (50, 100)- (距离Y轴水平距离, 距离X轴垂直距离)
   :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  # 获取被放图片的大小(母图)
   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):
   # 获取当前文件夹中所有JPG图像
   # im_list = [Image.open(fn) for fn in listdir() if fn.endswith('.jpg')]

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)}")

# 图片转化尺寸(注:此业务中,origin和result均为尺寸比例相同的图片(宽高比相同的图片))
   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}, height = {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 = './1000x966'
   root_path = './500x841'
   # root_path = './1000x667'

size_data = 1280  # 原图重制尺寸值 TODO 实现图片重制大小的时候按比例进行宽高的缩放
   origin_img_path = os.path.join(root_path, 'origin_image.png')
   result_img_path = os.path.join(root_path, 'result_image.png')
   face_img_path = os.path.join(root_path, 'face_image.png')
   stitching_img_path = os.path.join(root_path, 'stitching_.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,
                         # coordinate=(100, 500)
                         )
   logger.info(f"--- end --- res = {res}")

来源:https://blog.csdn.net/xuezhangjun0121/article/details/119060013

标签:Python,图像处理,图片拼接和堆叠
0
投稿

猜你喜欢

  • PyQt5实现拖放功能

    2023-08-06 11:52:56
  • 如何快捷地实现分页显示功能?

    2010-01-01 15:08:00
  • python 集合 并集、交集 Series list set 转换的实例

    2023-12-16 10:53:34
  • Python模块的制作方法实例分析

    2021-09-06 05:57:26
  • ASP读取Exif信息无组件实现过程

    2009-02-09 12:52:00
  • QML使用Python的函数过程解析

    2021-12-10 19:20:31
  • 安装MySQL的步骤和方法

    2009-07-30 08:38:00
  • 分享给Python新手们的几道简单练习题

    2021-08-01 22:05:14
  • php遍历CSV类实例

    2023-11-01 23:46:26
  • Python3 json模块之编码解码方法讲解

    2021-07-15 17:29:15
  • pytorch模型的保存加载与续训练详解

    2022-03-27 08:45:08
  • python使用magic模块进行文件类型识别方法

    2022-07-12 18:15:26
  • Python编写淘宝秒杀脚本

    2021-01-31 23:23:42
  • python实现图像全景拼接

    2023-05-08 13:29:24
  • Python boxplot 用法详解

    2021-10-24 07:56:11
  • Python使用monkey.patch_all()解决协程阻塞问题

    2021-05-11 17:27:19
  • python Flask 装饰器顺序问题解决

    2022-09-30 09:16:42
  • 学习完全掌握纯CSS布局网页

    2008-05-28 17:14:00
  • python实现PyEMD经验模态分解残差量分析

    2022-06-22 05:26:17
  • Django1.11配合uni-app发起微信支付的实现

    2023-12-18 13:22:22
  • asp之家 网络编程 m.aspxhome.com