gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
PYthon code to concatenate 4 images
- User
PYthon code to concatenate 4 images
- Assistant
Certainly! Here's a simple Python example that concatenates 4 images either horizontally or vertically using the
Pillowlibrary:from PIL import Image # Open the images img1 = Image.open('image1.jpg') img2 = Image.open('image2.jpg') img3 = Image.open('image3.jpg') img4 = Image.open('image4.jpg') # Resize images to the same size (optional but recommended) width, height = img1.size img2 = img2.resize((width, height)) img3 = img3.resize((width, height)) img4 = img4.resize((width, height)) # Concatenate horizontally total_width = width * 4 max_height = height new_img = Image.new('RGB', (total_width, max_height)) new_img.paste(img1, (0, 0)) new_img.paste(img2, (width, 0)) new_img.paste(img3, (width * 2, 0)) new_img.paste(img4, (width * 3, 0)) new_img.save('concatenated_horizontal.jpg') new_img.show()If you want to concatenate vertically:
# Concatenate vertically total_height = height * 4 max_width = width new_img_vertical = Image.new('RGB', (max_width, total_height)) new_img_vertical.paste(img1, (0, 0)) new_img_vertical.paste(img2, (0, height)) new_img_vertical.paste(img3, (0, height * 2)) new_img_vertical.paste(img4, (0, height * 3)) new_img_vertical.save('concatenated_vertical.jpg') new_img_vertical.show()