Pythonで画像を一括連結する方法は何ですか?

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を結合された画像の出力ファイルのパスに置き換えてください。この関数を実行すると、フォルダ内のすべての画像が水平方向に結合され、指定された出力ファイルに保存されます。

bannerAds