[Python] 纯文本查看 复制代码 from PIL import Image
def print_photos(input_image, output_image, rows, cols):
# 打开输入图片
img = Image.open(input_image)
# 获取图片尺寸
width, height = img.size
# 计算每张图片的尺寸
photo_width = width // cols
photo_height = height // rows
# 创建一个新的空白图片,用于存放排板后的图片
new_img = Image.new('RGB', (photo_width * cols, photo_height * rows))
# 遍历每个格子,将图片粘贴到新的图片上
for i in range(rows):
for j in range(cols):
box = (j * photo_width, i * photo_height, (j + 1) * photo_width, (i + 1) * photo_height)
new_img.paste(img.crop(box), (j * photo_width, i * photo_height))
# 保存排板后的图片
new_img.save(output_image)
# 调用函数,将1寸照片排板到6寸相纸上打印
print_photos('input_image.jpg', 'output_image.jpg', 4, 6)
|