본문 바로가기
카테고리 없음

이미지(url) 세로로 합치는 함수

by 저긍 2025. 6. 13.
반응형

from PIL import Image
import requests
from io import BytesIO

def stitch_images(image_urls, direction='vertical'):
    images = [Image.open(BytesIO(requests.get(url).content)).convert("RGB") for url in image_urls]

    if direction == 'vertical':
        total_width = max(img.width for img in images)
        total_height = sum(img.height for img in images)
        combined_img = Image.new("RGB", (total_width, total_height), color=(255, 255, 255))

        y_offset = 0
        for img in images:
            combined_img.paste(img, (0, y_offset))
            y_offset += img.height
    elif direction == 'horizontal':
        total_width = sum(img.width for img in images)
        total_height = max(img.height for img in images)
        combined_img = Image.new("RGB", (total_width, total_height), color=(255, 255, 255))

        x_offset = 0
        for img in images:
            combined_img.paste(img, (x_offset, 0))
            x_offset += img.width
    else:
        raise ValueError("vertical or horizontal")

    return combined_img


image_urls = [
    "https://",
    "https://",
    "https://",
    "https://"
]

stitched = stitch_images(image_urls, direction='vertical')
stitched.save("OutPut.png")
반응형