Gimp Python-Fu


A folder full of jpeg images of different sizes that I needed to resize to 300x300. Not wanting to open each image in Gimp - Tools - Transform Tools - Scale I was looking for a more automated solution. Gimp’s Python-Fu, Google and Stackoverflow with kind souls sharing their expertise to the resuce:

Gimp Python-Fu

  • In Gimp go to Filters - Python-Fu - Console
  • Hit Browse to find available Python methods - searched for scale and found gimp_image_scale, which seemed to do what I wanted.

With some more Googling I devised the following, which solves my problem. Pop into console and hit enter. See main sources below for proper credit.

import os

def get_files(path):
     for (dir, _, filenames) in os.walk(path):
         for filename in filenames:
             yield os.path.join(dir, filename)

list_files = get_files('/my_various_images')

for filename in list_files:
    image = pdb.file_jpeg_load(filename, filename)
    pdb.gimp_image_scale(image, 300, 300)
    new_img = pdb.gimp_image_duplicate(image)
    layer = pdb.gimp_image_merge_visible_layers(new_img, CLIP_TO_IMAGE)
    pdb.gimp_file_save(new_img, layer, filename + '.scaled.jpg', '?')

Main sources