python
主页 > 脚本 > python >

python实现碑帖图片横向拼接

2021-03-07 | 秩名 | 点击:

一、原图

二、拼接效果(按照书法的从右往左顺序)

三、Python代码

#Collection of calligraphy characters
import os
from PIL import Image

if __name__ == '__main__':
  im_list = []
  path = r"C:UsersAdministratorDesktop�"
  pathlist = os.listdir(path)
  for fn in reversed(pathlist):
    if fn.endswith('.jpg'):
      im_list.append(Image.open(path + os.sep + fn))

  width = 0
  height = 0
  for img in im_list:
    # 单幅图像尺寸
    w, h = img.size
    width += w
    # 取最大的宽度作为拼接图的宽度
    height= max(height, h)

  # 创建空白长图
  result = Image.new(im_list[0].mode, (width, height), 0xffffff)
  # 拼接图片
  width = 0
  for img in im_list:
    w, h = img.size
    # 图片水平居中
    result.paste(img, box=(width,round(height / 2 - h / 2)))
    width += w
  # 保存图片
  result.save(r'C:UsersAdministratorDesktop拼接长图.jpg')

原文链接:https://blog.csdn.net/weixin_43756157/article/details/114386206
相关文章
最新更新