# cat merge.py
import sys
from PIL import Image
def merge_images(image1_path, image2_path, output_path):
# 打开两个图片
img1 = Image.open(image1_path)
img2 = Image.open(image2_path)
# 获取每个图像的尺寸
width1, height1 = img1.size
width2, height2 = img2.size
# 创建一个新的图像,宽度是两个图像的宽度之和,高度是两者之间较大的那个
new_width = width1 + width2
new_height = max(height1, height2)
# 创建一个新的空白图像(白色背景)
new_img = Image.new("RGB", (new_width, new_height), (255, 255, 255))
# 将两个图像粘贴到新图像上
new_img.paste(img1, (0, 0))
new_img.paste(img2, (width1, 0))
# 保存新图像
new_img.save(output_path)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python merge_images.py <image1_path> <image2_path> <output_path>")
sys.exit(1)
image1_path = sys.argv[1]
image2_path = sys.argv[2]
output_path = sys.argv[3]
merge_images(image1_path, image2_path, output_path)