How can I batch add square padding to images of different sizes and aspect ratios?
Asked 2/13/2020
1 views
2 answers
0
I have a group of images with mixed dimensions and aspect ratios, and I want each one placed on a square background without cropping or stretching. For each image, the square canvas should be based on that image’s largest dimension, with the original image centered. For example, a 1400×600 image should become 1400×1400, and a 500×1000 image should become 1000×1000. The added area can be transparent or white. Is there a way to do this in batch using GIMP, Photoshop, or another tool?
Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0
Photography Stack Exchange contributor
6y ago
2 Answers
0
Generated from our catalog & community — verify before relying on it.
Yes. What you want is batch canvas expansion, not resizing the image content. For each file, the canvas should be enlarged to a square whose width and height equal the image’s longer side, then the image should be centered and the new area filled with transparency or white.
From the community answers, GIMP can do this with a Python-Fu script that:
- reads each image’s width and height
- finds the larger dimension
- resizes the canvas to max(width, height) × max(width, height)
- centers the existing layer/image on the new square canvas
That gives results like 1400×600 → 1400×1400 and 500×1000 → 1000×1000, exactly as described.
In general terms, many editors can do this through scripting or batch actions, but the key is to use canvas size/image extent rather than image resize. If you’re using GIMP, a Python-Fu batch script is the most direct solution based on the provided answers.
Recommended products
UniqueBot
AI6y ago
0
To enlarge to square all images:
- Copy this GIMP python-fu script:
#!/usr/bin/env python
from gimpfu import *
def square_all_images(dummy_image, dummy_drawable):
pdb.gimp_context_push()
# Get the list of all opened images
images = gimp.image_list()
image_number = len(images)
# Loop through each image
for i in range(image_number):
image = images[i]
drawable = image.active_layer
pdb.gimp_progress_update((i * 1.0) / image_number)
pdb.gimp_progress_set_text("Processing image #" + str(i + 1) + " over " + str(image_number) + "...")
# Get image size
width = image.width
height = image.height
# Calculate the maximum dimension
max_dim = max(width, height)
# Calculate the new dimensions
new_width = max_dim
new_height = max_dim
# Calculate the offset
offset_x = (max_dim - width) // 2
offset_y = (max_dim - height) // 2
# Enlarge the canvas
image.resize(new_width, new_height, offset_x, offset_y)
drawable.resize(new_width, new_height, offset_x, offset_y)
# Save the image file
filename = pdb.gimp_image_get_filename(image)
pdb.gimp_file_save(image, drawable, filename, '?')
# Refresh the image to see the changes
pdb.gimp_context_pop()
pdb.gimp_displays_flush()
# Define the parameters for the script
register(
"python_fu_square_all_images",
"Curves on all images",
"Curves on all images and save; identical values will be ignored",
"Fabrice TIERCELIN",
"Fabrice TIERCELIN",
"2024",
"<Image>/Filters/Enlarge to square all images",
"*",
[],
[],
square_all_images
)
# Main function
main()
- Paste it into a text file
- Save it with a
.pyextension - Put this file on your
C:\Users\YourName\AppData\Roaming\GIMP\2.10\plug-insfolder (more information below if it doesn't work) - Start GIMP
- Open all your image files by drag and drop
- Go on Filter → Enlarge to square all images
- Launch the script → all the files are already saved as square
- Quit GIMP ignoring the prompts
For systems other than Windows, look at the settings in GIMP:
- Click on Edit
- Click on Preferences
- Go on left pane; at bottom
- Click on Folders
- Click on Plug-ins
- Click on Add a new folder
Successfully tested on GIMP 2.10.36 on Windows 10.
Originally by user115930. Source · Licensed CC BY-SA 4.0
user115930
2y ago
Your Answer
Related Questions
How can I batch-add transparent padding around images programmatically?
How can I batch-convert product photos to square images without cropping on Windows?
How can I batch-fit mixed portrait and landscape photos onto a square canvas?
How can I batch-crop scanned images evenly from all sides without resizing?
Why do smartphones offer their highest resolution in 4:3 instead of 1:1?