티스토리 뷰

반응형

이미지 3장이 있다고 할때 우선은 쉽게 코드를 만들어보면 아래처럼

from PIL import Image

# Open images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image3 = Image.open('image3.jpg')

# Get dimensions of the first image
width, height = image1.size

# Create a new image with height equal to the combined height of all images
combined_height = height * 3  # Adjust according to the number of images
combined_image = Image.new('RGB', (width, combined_height))

# Paste each image onto the combined image vertically
combined_image.paste(image1, (0, 0))
combined_image.paste(image2, (0, height))
combined_image.paste(image3, (0, height * 2))

# Save the combined image
combined_image.save('combined_image_vertical.jpg')

이렇게 코드를 만들면

1번 이미지와 2번 이미지와 3번 이미지가 차곡차곡 세로로 연결이 되는데요

문제는 아마 1,2,3 의 이미지가 동일하다는 전제에서 그렇게 되겠죠

그럼 1,2,3 중에 가로 사이즈가 가장 큰 이미지를 기준으로 세로 높이를 새로 구성해서만들어야겠죠.

from PIL import Image

# Open images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image3 = Image.open('image3.jpg')

# Get the width of the widest image
max_width = max(image1.width, image2.width, image3.width)

# Resize images to match the widest image
image1_resized = image1.resize((max_width, image1.height))
image2_resized = image2.resize((max_width, image2.height))
image3_resized = image3.resize((max_width, image3.height))

# Create a blank image to merge onto
combined_height = sum([image1_resized.height, image2_resized.height, image3_resized.height])
combined_image = Image.new('RGB', (max_width, combined_height))

# Paste resized images onto the blank image vertically
combined_image.paste(image1_resized, (0, 0))
combined_image.paste(image2_resized, (0, image1_resized.height))
combined_image.paste(image3_resized, (0, image1_resized.height + image2_resized.height))

# Save the combined image
combined_image.save('combined_image_vertical.jpg')

근데 여기서 또 문제는 

기존의 가로 세로 비율이 유지되지 않고 가로만 리사이즈 변경되니까 이걸 다시 맞춰야하겠죠

저 이미지중에서 최대 가로사이즈를 구한 것이기 때문에 

다른것들은 최대보다 당연히 작거나 같겠죠

from PIL import Image

# Open images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image3 = Image.open('image3.jpg')

# Get the width of the widest image
max_width = max(image1.width, image2.width, image3.width)

# Calculate the new height for maintaining aspect ratio
new_height1 = int(image1.height * (max_width / image1.width))
new_height2 = int(image2.height * (max_width / image2.width))
new_height3 = int(image3.height * (max_width / image3.width))

# Resize images to match the widest image with adjusted height
image1_resized = image1.resize((max_width, new_height1))
image2_resized = image2.resize((max_width, new_height2))
image3_resized = image3.resize((max_width, new_height3))

# Create a blank image to merge onto
combined_height = sum([new_height1, new_height2, new_height3])
combined_image = Image.new('RGB', (max_width, combined_height))

# Paste resized images onto the blank image vertically
combined_image.paste(image1_resized, (0, 0))
combined_image.paste(image2_resized, (0, new_height1))
combined_image.paste(image3_resized, (0, new_height1 + new_height2))

# Save the combined image
combined_image.save('combined_image_vertical.jpg')

근데 이건 3개의 이미지인 경우이고 이미지가 몇개가 될지는 모르니까

배열과 for문을 같이 사용해서 만들면 좀더 세련되 보이는 코드가 나옵니다.

from PIL import Image

# Define image file names in a list
image_files = ['image1.jpg', 'image2.jpg', 'image3.jpg']

# Open images and get their dimensions
images = [Image.open(image_file) for image_file in image_files]
widths = [image.width for image in images]

# Get the width of the widest image
max_width = max(widths)

# Calculate the new heights for maintaining aspect ratio
new_heights = [int(image.height * (max_width / image.width)) for image in images]

# Resize images to match the widest image with adjusted height
resized_images = [image.resize((max_width, new_height)) for image, new_height in zip(images, new_heights)]

# Create a blank image to merge onto
combined_height = sum(new_heights)
combined_image = Image.new('RGB', (max_width, combined_height))

# Paste resized images onto the blank image vertically
y_offset = 0
for resized_image, new_height in zip(resized_images, new_heights):
    combined_image.paste(resized_image, (0, y_offset))
    y_offset += new_height

# Save the combined image
combined_image.save('combined_image_vertical.jpg')
반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함