上記postでChatGPTが使っていたPythonコード
(*パス名は変更しています.)
# Re-import necessary libraries
from PIL import Image
# Redefine the image paths after reset
img1_path = '/pass/xxx.png'
img2_path = '/pass/xxx2.png'
img3_path = '/pass/xxx3.png'
img4_path = '/pass/xxx4.png'
img5_path = '/pass/xxx5.png'
# Open the images
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
img3 = Image.open(img3_path)
img4 = Image.open(img4_path)
img5 = Image.open(img5_path)
# Define the size of the canvas based on the image dimensions, including margins between images
# Calculate the maximum height and width among all images
max_width = max(img.width for img in [img1, img2, img3, img4, img5])
max_height = max(img.height for img in [img1, img2, img3, img4, img5])
# Define margins
margin = 10 # 10 pixel margin
# Calculate the total canvas size including margins
canvas_width = (max_width 3) + (margin 4)
canvas_height = (max_height 2) + (margin 3)
# Create a new blank white canvas
collage = Image.new('RGB', (canvas_width, canvas_height), 'white')
# Define positions for images to avoid overlap and include margins
# Positions are calculated to center the images within each designated grid space
positions = [
(margin, margin), # Image 1 (top left)
(max_width + (margin * 2), margin), # Image 2 (top center)
((max_width 2) + (margin 3), margin), # Image 3 (top right)
(margin, max_height + (margin * 2)), # Image 4 (bottom left)
(max_width + (margin 2), max_height + (margin 2)), # Image 5 (bottom center)
# The bottom right position will remain empty since we have only 5 images
]
# Resize images to fit the grid and maintain aspect ratio
for img, pos in zip([img1, img2, img3, img4, img5], positions):
# Maintain the aspect ratio
aspect_ratio = img.width / img.height
new_width = max_width
new_height = round(new_width / aspect_ratio)
# If height after resizing is more than max, resize based on height
if new_height > max_height:
new_height = max_height
new_width = round(new_height * aspect_ratio)
# Resize the image
resized_img = img.resize((new_width, new_height), Image.ANTIALIAS)
# Calculate exact position to center the image in the grid space
exact_x = pos[0] + (max_width - new_width) // 2
exact_y = pos[1] + (max_height - new_height) // 2
# Paste the image onto the canvas
collage.paste(resized_img, (exact_x, exact_y))
# Save the final collage
collage_path = '/pass/xxx_re.png'
collage.save(collage_path)
collage_path
--------------------
★上記コードに下記の要素を足してもらった.
・縦型のリザルト画像にしてほしい。
・白の余白面積が最小になるように、各画像の大きさや配置を工夫してほしい。
# Re-import necessary libraries
from PIL import Image
# Redefine the image paths after reset
img1_path = '/pass/xxx01.png'
img2_path = '/pass/xxx02.png'
img3_path = '/pass/xxx03.png'
img4_path = '/pass/xxx04.png'
img5_path = '/pass/xxx05.png'
# Open the images
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
img3 = Image.open(img3_path)
img4 = Image.open(img4_path)
img5 = Image.open(img5_path)
# The images will be arranged vertically, so we need to determine the width of the widest image
max_width = max(img.width for img in [img1, img2, img3, img4, img5])
# Define margins
margin = 10 # 10 pixel margin
# Calculate the total height with margins
total_height = sum(img.height for img in [img1, img2, img3, img4, img5]) + (margin * 6)
# Create a new blank white canvas
collage_vertical = Image.new('RGB', (max_width + (2 * margin), total_height), 'white')
# Initialize the y position for pasting images
current_height = margin
# Function to resize image to fit the width
def resize_to_width(image, base_width):
w_percent = (base_width / float(image.size[0]))
h_size = int((float(image.size[1]) * float(w_percent)))
return image.resize((base_width, h_size), Image.ANTIALIAS)
# Resize images to fit the width and paste them with minimal margins
for img in [img1, img2, img3, img4, img5]:
resized_img = resize_to_width(img, max_width)
collage_vertical.paste(resized_img, (margin, current_height))
current_height += resized_img.height + margin
# Save the final vertical collage
collage_vertical_path = '/pass/xxx_re01.png'
collage_vertical.save(collage_vertical_path)
collage_vertical_path
Comments