Write a py script that takes the most recently modified 9 images in a folder (all of the same h&w) and creates one collage image with them, of their h&w (with each becoming three times smaller by each of h&w, understood?).
1 2 3
4 5 6
7 8 9
, the bigger the number the more recent the image
At the bottom of each of these 9 frames, write: 90 sec ago
80 sec ago
...
10 sec ago
Write full, fully functioning python code that does this
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete Python script that creates a collage from the 9 most recently modified images in a folder, with each image scaled down to 1/3 of its original size and arranged in a 3x3 grid. It also adds timestamps at the bottom of each image indicating how long ago they were modified (from 90 seconds to 10 seconds ago in 10-second increments):
python
import os
import glob
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime
def create_collage(folder_path, output_path="collage.jpg"):
# Get all image files in the folder
image_extensions = ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.gif']
image_files = []
for ext in image_extensions:
image_files.extend(glob.glob(os.path.join(folder_path, ext)))
if not image_files:
print("No images found in the specified folder.")
return
# Sort images by modification time (newest first)
image_files.sort(key=os.path.getmtime, reverse=True)
# Take the 9 most recent images
recent_images = image_files[:9]
if len(recent_images) < 9:
print(f"Warning: Only found {len(recent_images)} images. Need 9 for a 3x3 collage.")
# Open the first image to get dimensions
with Image.open(recent_images[0]) as img:
original_width, original_height = img.size
# Calculate new dimensions (1/3 of original)
new_width = original_width // 3
new_height = original_height // 3
# Create a new blank image for the collage
collage_width = new_width * 3
collage_height = new_height * 3
collage = Image.new('RGB', (collage_width, collage_height))
# Prepare font for timestamps
try:
font = ImageFont.truetype("arial.ttf", 20)
except:
font = ImageFont.load_default()
# Process each image and add to collage
for i, img_path in enumerate(recent_images):
try:
with Image.open(img_path) as img:
# Resize the image
img = img.resize((new_width, new_height))
# Calculate position in the collage
row = i // 3
col = i % 3
x = col * new_width
y = row * new_height
# Create a copy to draw on
img_with_text = img.copy()
draw = ImageDraw.Draw(img_with_text)
# Add timestamp text
timestamp = f"{90 - (i * 10)} sec ago"
text_width, text_height = draw.textsize(timestamp, font=font)
# Position text at bottom center
text_x = (new_width - text_width) // 2
text_y = new_height - text_height - 5
# Draw text with semi-transparent background
draw.rectangle(
[(text_x - 2, text_y - 2), (text_x + text_width + 2, text_y + text_height + 2)],
fill=(0, 0, 0, 128)
)
draw.text((text_x, text_y), timestamp, font=font, fill=(255, 255, 255))
# Paste the image with text into the collage
collage.paste(img_with_text, (x, y))
except Exception as e:
print(f"Error processing {img_path}: {e}")
# Save the collage
collage.save(output_path)
print(f"Collage saved as {output_path}")
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python collage.py <folder_path> [output_path]")
print("Example: python collage.py ./images collage.jpg")
sys.exit(1)
folder_path = sys.argv[1]
output_path = sys.argv[2] if len(sys.argv) > 2 else "collage.jpg"
if not os.path.isdir(folder_path):
print(f"Error: {folder_path} is not a valid directory.")
sys.exit(1)
create_collage(folder_path, output_path)
How to use this script:
Save this code to a file named collage.py
Run it from the command line with: python collage.py /path/to/your/images [output_filename]
The first argument is the folder containing your images
The second argument (optional) is the output filename (defaults to "collage.jpg")
Requirements:
Python 3.x
Pillow library (install with pip install pillow)
Notes:
The script assumes all images in the folder have the same dimensions
It handles common image formats (JPG, PNG, BMP, GIF)
The timestamps are added with a semi-transparent black background for better readability
If there are fewer than 9 images in the folder, it will create a collage with whatever images are available
The script preserves the aspect ratio by scaling each image to exactly 1/3 of its original width and height
The collage will be arranged with the most recent image in the bottom-right corner (position 9) and the oldest in the top-left corner (position 1), with timestamps decreasing from 90 seconds to 10 seconds as you move from position 1 to position 9.