Python Plug-In for Gimp

See the entire codebase here on GitHub

A bit of background and what this plug-in does

I'm a big fan of analog photography. I always take my camera with me when I'm going to new places. I like to post some of my pictures on Instagram, and one thing that I found repetitive was editing my pictures to be on a square canvas with chosen background color which I can later upload. So I decided to automate it. Using Python and GIMP’s scripting capabilities (Python-Fu), I wrote a plug-in that:

  • Takes an image in jpg format.
  • Finds the larger dimension (width or height) of that image.
  • Creates a square canvas based on found dimension and adds padding which can be specified.
  • Adds specified background color.
  • Adds the original image in the center of the canvas.
  • Exports the new image to jpg format.

Key parts of the plug-in


my_image = pdb.gimp_file_load(my_image_path, my_image_path)
                                
Loading chosen image

max_dim = max(my_image.width, my_image.height)
canvas_size = max_dim + padding
img = gimp.Image(canvas_size, canvas_size, RGB)
                                
Creating a square canvas

gimp.set_background(background_color_r, background_color_g, background_color_b)
                                
Setting a background color

x_offset = (canvas_size - my_image.width) // 2
y_offset = (canvas_size - my_image.height) // 2
pdb.gimp_layer_set_offsets(overlay, x_offset, y_offset)
                                
Centering the original image

pdb.file_jpeg_save(img, flattened, export_path, export_path, 0.9, 0, 0, 0, "", 0, 1, 0, 0)
                                
Exporting final image
An unhandled error has occurred. Reload 🗙