溫馨提示×

Python怎么批量拼接圖片

小億
118
2024-01-26 14:03:17
欄目: 編程語言

在Python中,可以使用PIL(Python Imaging Library)庫來進行圖像處理和拼接。以下是一種批量拼接圖片的方法:

from PIL import Image
import os

def join_images(input_folder, output_file):
    images = []
    for filename in os.listdir(input_folder):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            images.append(Image.open(os.path.join(input_folder, filename)))

    widths, heights = zip(*(i.size for i in images))

    total_width = sum(widths)
    max_height = max(heights)

    new_image = Image.new('RGB', (total_width, max_height))

    x_offset = 0
    for image in images:
        new_image.paste(image, (x_offset, 0))
        x_offset += image.width

    new_image.save(output_file)

# 使用示例
input_folder = "path/to/input/folder/"
output_file = "path/to/output/file.jpg"
join_images(input_folder, output_file)

請將input_folder替換為包含要拼接圖片的文件夾的路徑,將output_file替換為輸出拼接圖片的文件路徑。運行該函數后,會將文件夾中的所有圖片按照水平方向拼接,并保存為指定的輸出文件。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女