python图片合并

python图片合并

import os
import PIL.Image as Image

IMAGE_SIZE = 1024 # 宽高都为1024
IMAGE_ROW, IMAGE_COLUMN = 24, 19
IMAGE_SIZE_W, IMAGE_SIZE_H = 20001, 25001
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
clip_img_path_2020 = os.path.join(root, 'clip') # 小图片路径
IMAGE_SAVE_PATH = os.path.join(data_path, 'img_merge', 'clip_2020.jpg') # 保存合并图片路径

to_image = Image.new('RGB', (IMAGE_SIZE_W, IMAGE_SIZE_H))  # 创建合并图片大小
for y in range(0, IMAGE_ROW + 1):  # 循环遍历,把每张图片按顺序粘贴到对应位置上
    for x in range(0, IMAGE_COLUMN + 1):
        file_name = str(y) + '_' + str(x) + ".jpg"
        img_name = os.path.join(clip_img_path_2020, file_name)
        from_image = Image.open(img_name) # 读取各个图片
        to_image.paste(from_image, ((x) * IMAGE_SIZE, (y) * IMAGE_SIZE))  # 粘贴image到im的position(左上角)位置。(w,h)
to_image.save(IMAGE_SAVE_PATH) # 保存图片

你可能感兴趣的:(python数据分析基础,python,图片合并)