在Jupyter Notebook(Python)中如何统一图片尺寸并保持比例及选定适配尺寸
Hey there! Let's work through this problem together—you’ve already knocked out the tough part of whittling 1478 diverse images down to just 4 distinct sizes, so we’re well on our way to finding a solid solution for uniform sizing while preserving aspect ratios.
First, let’s break down the aspect ratios of your remaining 4 sizes to simplify things:
(3120, 4160): Aspect ratio = 3120/4160 = 0.75 → 3:4(2340, 4160): Aspect ratio ≈ 2340/4160 = 0.5625 → 9:16(3264, 2448): Aspect ratio ≈ 3264/2448 = 1.333 → 4:3(3264, 2441): This is almost identical to 2448 (likely a minor capture error), so we can treat it as 4:3 too
Below are three practical approaches to pick a uniform size that works for all your images:
Option 1: Use a Fixed Canvas (With Padding)
This method picks a single canvas size, scales each image to fit within it while keeping its aspect ratio, and fills any empty space with a solid color (like black or white). It’s great if you need a strict uniform dimension (e.g., for model training).
Steps to implement:
- Check how many images fall into each aspect ratio group—prioritize the group with the most images when setting your canvas size (e.g., if 4:3 images are the majority, build around that).
- Choose a canvas size that can accommodate the largest scaled version of your images. For example:
- If you pick a square canvas of
(4160, 4160)(matching your largest existing long edge), all images will fit with padding. - Or if you want a smaller canvas, go with
(3264, 3264)(matching the 4:3 group’s width) to minimize padding for most images.
- If you pick a square canvas of
Python code example (using PIL):
from PIL import Image, ImageOps def resize_with_padding(image_path, target_canvas_size): # Open the image img = Image.open(image_path) # Scale image to fit the canvas, then center it with padding resized_img = ImageOps.fit( img, target_canvas_size, method=Image.Resampling.LANCZOS, # High-quality resampling bleed=0.0, centering=(0.5, 0.5) # Center the image in the canvas ) return resized_img # Example: Resize to a 4160x4160 square canvas resized_image = resize_with_padding("your_image.jpg", (4160, 4160)) resized_image.save("resized_with_padding.jpg")
Option 2: Uniform Short or Long Edge
If you don’t need a strict fixed canvas, you can scale all images so they share the same short edge (or long edge) length. This keeps aspect ratios intact without padding, though final image dimensions will still vary slightly.
Steps to implement:
- Pick a target length for the short edge (e.g., 2000 pixels) or long edge (e.g., 4000 pixels).
- Scale each image proportionally to match that target length.
Python code example:
from PIL import Image def resize_to_fixed_edge(image_path, fixed_edge_length): img = Image.open(image_path) width, height = img.size if width < height: # Short edge is width—scale width to fixed length new_width = fixed_edge_length new_height = int(height * (fixed_edge_length / width)) else: # Short edge is height—scale height to fixed length new_height = fixed_edge_length new_width = int(width * (fixed_edge_length / height)) # Resize with high-quality sampling return img.resize((new_width, new_height), Image.Resampling.LANCZOS) # Example: Resize all images to have a short edge of 2000 pixels resized_image = resize_to_fixed_edge("your_image.jpg", 2000) resized_image.save("fixed_short_edge.jpg")
Option 3: Group by Aspect Ratio
If your downstream task (like model training or image display) allows for multiple uniform sizes, group images by their aspect ratios and resize each group to a standard size for that ratio. This preserves the most image detail and avoids padding entirely.
Example grouping:
- 3:4 group: Resize to
(1560, 2080)(half the original 3120x4160 to save space) - 9:16 group: Resize to
(1170, 2080)(half the original 2340x4160) - 4:3 group: Resize to
(1632, 1224)(half the original 3264x2448)
Quick tip:
Always test with a few sample images first! Check if the resized images are sharp enough for your needs—avoid scaling down too much if you need to retain fine details.
内容的提问来源于stack exchange,提问作者keerat singh

