最近由于测试工作的原因,需要将大量截图上传到jira上,感觉有点费时,于是写了这个脚本,希望能提高点工作效率吧

脚本1

适用范围:多张图合并成一张一行多列的长图

# coding=utf-8
# 横向拼接长图
import os
from PIL import Image

if __name__ == '__main__':
    path = "/Users/luffy.y.wan_sp/Downloads/picture/Journey_Navigation_on_ Application_Confirmation _Page/iOS/No_Plan_Balance_Instalment/"
    picName_list = os.listdir(path)
    image_list = [fn for fn in picName_list if fn.endswith('.png')]
    new_list = sorted(image_list, key=lambda x: int(x[-5:-4]))
    im_list = [Image.open(path + '/%s' % i) for i in new_list]

    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(path + 'ijnnb.png')

脚本2:

适用范围:多张图合并成一张多行多列的长图

# coding=utf-8
# 横向拼接长图
import PIL.Image as Image
import os

# 图片集地址
IMAGES_PATH = '/Users/luffy.y.wan_sp/Downloads/picture/Journey_Navigation_on_ Application_Confirmation _Page/iOS/HK_Product_Shelf_Balance_Instalment/'
# 图片的尺寸(可通过详细信息查看)
IMAGE_WIDTH = 1242
IMAGE_HEIGHT = 2208
# 转化的行列数
IMAGE_ROW = 1
IMAGE_COLUMN = 10
# 图片转换后的地址
IMAGE_SAVE_PATH = IMAGES_PATH + 'ijhppb.png'

# 获取图片集地址下的所有图片名称
picName_list = os.listdir(IMAGES_PATH)
# 过滤后缀名不是.png的图片
image_list = [fn for fn in picName_list if fn.endswith('.png')]
# 将图片按文件名中的数字排序
image_names = sorted(image_list, key=lambda x: int(x[-5:-4]))

# 简单对参数的设定和实际图片集的大小进行数量判断
if len(image_names) != IMAGE_ROW * IMAGE_COLUMN:
    raise ValueError("合成图片的参数和要求的数量不匹配!")


# 定义图像拼接函数
def image_compose():
    # 创建一个新图
    to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_WIDTH, IMAGE_ROW * IMAGE_HEIGHT))
    # 循环遍历,把每张图片按顺序粘贴到对应位置上
    for y in range(1, IMAGE_ROW + 1):
        for x in range(1, IMAGE_COLUMN + 1):
            from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
                (IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS)
            to_image.paste(from_image, ((x - 1) * IMAGE_WIDTH, (y - 1) * IMAGE_HEIGHT))
    # 保存新图
    return to_image.save(IMAGE_SAVE_PATH)


image_compose()