1
\$\begingroup\$

I have a batch of images that are of different size and aspect ratio.

I need to give them all a square canvas (x=y) with the image in the center. With the imaging software that I am using (photoshop & GIMP), I can only find the option to set and center a batch of images in a fixed canvas. The problem with this is that the images that is to small for the canvas, gets a canvas that is too large, and the images that has an aspect (y, x or both) that is larger then the canvas will exceed the borders of the canvas.

Is it anyway to get a photo editing software to batch edit photos in to a square aspect ratio where the canvas size of each photo is determined by its largest aspect dimension? Meaning if a photo is 1400x600, the canvas for that image becomes 1400x1400 and a image that is 500x1000 becomes 1000x1000, with the image in center and the empty space generated by the new aspect in transparent or white.

I might be using the word "canvas" in a wrong way here. Think images with different aspect ratios and size -> Images with equal aspect ratios but different size.

\$\endgroup\$
4

1 Answer 1

0
\$\begingroup\$

To enlarge to square all images:

  1. 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()
  1. Paste it into a text file
  2. Save it with a .py extension
  3. Put this file on your C:\Users\YourName\AppData\Roaming\GIMP\2.10\plug-ins folder (more information below if it doesn't work)
  4. Start GIMP
  5. Open all your image files by drag and drop
  6. Go on FilterEnlarge to square all images
  7. Launch the script → all the files are already saved as square
  8. Quit GIMP ignoring the prompts

For systems other than Windows, look at the settings in GIMP:

  1. Click on Edit
  2. Click on Preferences
  3. Go on left pane; at bottom
  4. Click on Folders
  5. Click on Plug-ins
  6. Click on Add a new folder

Successfully tested on GIMP 2.10.36 on Windows 10.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.