Create thumbnails maintaining aspect ratio. If the target box is 100x100 but the image…
- User
Create thumbnails maintaining aspect ratio. If the target box is 100x100 but the image is 200x100, resize to 100x50 and paste it centered on a 100x100 white background canvas for uniform output dimensions.
- Assistant
Providing Python thumbnail snippet
I'm preparing a concise Python example using Pillow to resize an image maintaining aspect ratio, then centering it on a fixed-size white background canvas (e.g., 100x100). This addresses the user's request clearly and efficiently. Below is a simple Python example using the Pillow library. It rescales an image to fit within a 100×100 box while preserving aspect ratio, then centers it on a white 100×100 canvas:
from PIL import Image def make_thumbnail(input_path, output_path, box_size=(100, 100)): # Open the original image img = Image.open(input_path).convert("RGB") orig_w, orig_h = img.size target_w, target_h = box_size # Compute the scale factor that best fits the image into box_size scale = min(target_w / orig_w, target_h / orig_h) new_w = int(orig_w * scale) new_h = int(orig_h * scale) # Resize the image with the computed dimensions resized = img.resize((new_w, new_h), Image.LANCZOS) # Create a new white canvas of the target size canvas = Image.new("RGB", box_size, "white") # Calculate positions to center the resized image offset_x = (target_w - new_w) // 2 offset_y = (target_h - new_h) // 2 # Paste the resized image onto the canvas canvas.paste(resized, (offset_x, offset_y)) # Save the thumbnail canvas.save(output_path) # Example usage: # make_thumbnail("input.jpg", "thumbnail.jpg", (100, 100))In your specific example, if the image is 200×100, it will be resized to 100×50, then pasted in the center of a 100×100 white background.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.