Skip to content

Instantly share code, notes, and snippets.

@flash1293
Created November 4, 2016 06:47
Show Gist options
  • Save flash1293/a0f2e1ed8812b71f1d669b45f6fddaa0 to your computer and use it in GitHub Desktop.
Save flash1293/a0f2e1ed8812b71f1d669b45f6fddaa0 to your computer and use it in GitHub Desktop.
Python script for removing a white background, cropping the image and saving it to disk in python
#This script iterates over all open images, removes everything of the color white, crops the image and saves it to disk
#Usage: Filters > Python-Foo > Console
#Doc: here https://www.gimp.org/docs/python/ and here http://oldhome.schmorp.de/marc/pdb/
#To get an overview over available functions, help(gimp), help(gimp.pdb) and gimp.pdb.<SOME FUNCTION>.params is important
#Gotcha: run_mode-parameters are set from the system to non-interactive
#e.g. the plug_in_autocrop function advertises to have 3 parameter, but in reality has only two
BASE_PATH = '/path/to/target/folder'
FILE_EXTENSION = '.png'
for idx, img in enumerate(gimp.image_list()):
layer = img.layers[0]
filename = BASE_PATH + str(idx) + FILE_EXTENSION
gimp.pdb.gimp_by_color_select(layer, 'white', 12, 0, TRUE, 0, 0, 0)
gimp.pdb.gimp_edit_clear(layer)
gimp.pdb.plug_in_autocrop(img, layer)
gimp.pdb.gimp_file_save(img, layer, filename, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment