将4张照片拼接为一张照片
from PIL import Image
# 打开四张照片
image1 = Image.open('lena.jpg')
image2 = Image.open('lena.jpg')
image3 = Image.open('lena.jpg')
image4 = Image.open('lena.jpg')
# 设置统一的尺寸
target_width = 300
target_height = 300
# 调整所有图片的大小
image1 = image1.resize((target_width, target_height))
image2 = image2.resize((target_width, target_height))
image3 = image3.resize((target_width, target_height))
image4 = image4.resize((target_width, target_height))
# 创建一个新的空白图像,宽度为两张图片的宽度之和,高度为两张图片的高度之和
new_image = Image.new('RGB', (target_width * 2, target_height * 2))
# 将四张图片按顺序粘贴到新的空白图像中
new_image.paste(image1, (0, 0))
new_image.paste(image2, (target_width, 0))
new_image.paste(image3, (0, target_height))
new_image.paste(image4, (target_width, target_height))
# 保存拼接后的图片
new_image.save('output.jpg')
# 显示拼接后的图片
new_image.show()