“Efficiency is intelligent laziness”
Scripting is a great way to spend ten times the amount of time to automate something as it would have taken to just do it in the first place. Sometimes it’s worth it because it saves you time in the long run, and sometimes it’s worth it because you’d lose your mind doing something that takes three words to describe over and over and over but is unnervingly complex for no reason apart from making your life miserable. Alas, like cleaning the litter box, you said you would do it, it needs to be done, and it’s going to happen a lot.
BEHOLD – my favorite photo editing suite: GIMP. Apart from being the poor man’s [insert trademarked photo editing suite you would probably only purchase if it were your profession to edit photos or create media as a graphic designer], GIMP is open-source, free, community driven, and SCRIPTABLE. That’s great if you’ve been voluntold that once a week for the foreseeable future you’ll be, for example: collecting the top six voted, full resolution, user-provided images per week of various image formats and making square, high compression JPEG thumbnail images of them to add to the company newsletter. Yes, that is a very specific example, and yes, it is of very low caliber functionality for a powerful suite like GIMP, but bear with me.
By default, all the GIMP documentation points to using Script-Fu, which is a dialect of scheme (no thanks), but it is also possible to write using the aptly named Python-fu (perfect). The documentation for Python-fu is a little lackluster (https://www.gimp.org/docs/python/) and does require some translation from the reference manuals (https://developer.gimp.org/api/2.0/), but it can be pretty rewarding and gets easier in time. What these docs do not explicitly tell you is that you can browse the procedural database by opening GIMP and navigating to Help->Procedure Browser in the program’s menu. Everything in there, and that’s basically an itemized list of everything you can do in a stock install of GIMP, can be addressed in your python script by pdb.insert_command_here(). You can test anything you want in there pythonically by navigating to Filters->Python-fu->Console which basically gives you a Python console with all the GIMP imports loaded. Write it out as a function definition and put the requisite wrapper around it, and you have yourself a custom plug-in script – which can even appear as a menu item in a location of your choosing. Add the ability to run this plugin from the command line, and you have automated your weekly monotony.
So lets walk though this image resizing script, shall we?
Gather your images
Open one up in GIMP to get started
Go through the motions of what you would usually do, but do not do it
Open the procedure browser
Find the procedure of the same command name
Open up the Python-fu console
Scale down the image
SUPER IMPORTANT NOTE – hyphens in the procedure browser become underscores in python-fu
Now that the image is scaled down, it needs to be squared up with a new background layer so skip ahead to the Procedure Browser to find a procedure that matches what you are trying to accomplish
After a long and circuitous perusing through the Procedure Browser translating menu clicks and background processes into what remains to be accomplished (HINT: read the full procedure definition AND additional information section), the rest of the script should come together. The remaining steps are listed below:
-Create the new layer
-Attach the new layer to the bottom layer of the image
-Center the new layer around the scaled image
-Color the background layer black
-Resize the canvas to fit the layer
-Flatten the image layers
-Save the file as a jpeg
Now that it works, wrap it up in a nice python function definition – Notice I’ve added a couple modifications, and changed variable names for brevity. You can trust me on why or consider it homework to find out.
The modifications also allowed the modified image to be saved to the directory it was loaded from
Go into your GIMP installation preferences
Get the path to your plug-ins directory
Copy the following script to a file inside that directory with the extension “.py” because it is a Python script. BE SURE TO SET THE FILE TO BE EXECUTABLE – a good old chmod 755 will work nicely. Restart GIMP and you will find…
#!/usr/bin/env python from gimpfu import * def blackThumb(*args): loadfile = args img = pdb.gimp_file_load(loadfile, loadfile) if img.base_type!=0: pdb.gimp_image_convert_rgb(img) square_size = 100 scale_factor = float(img.width)/square_size scale_factor = max(scale_factor,float(img.height)/square_size) pdb.gimp_image_scale(img,int(img.width/scale_factor),int(img.height/scale_factor)) bg = pdb.gimp_layer_new(img,square_size,square_size,0,"BlackBack",100,0) img.add_layer(bg,len(img.layers)+1) pdb.gimp_layer_translate(bg, 0-(bg.width-img.width)/2, 0-(bg.height-img.height)/2) pdb.gimp_image_set_active_layer(img,bg) drwbl = pdb.gimp_image_active_drawable(img) pdb.gimp_context_set_background((0,0,0)) pdb.gimp_drawable_fill(drwbl, 1) pdb.gimp_image_resize_to_layers(img) img.flatten() savename = img.filename+ ".thumb.jpg" pdb.file_jpeg_save(img, img.layers, savename, savename ,0.9, 0, 0, 0, "", 0, 0, 0, 0) register( "black_thumb", "Create a 100x100 jpeg thumbnail", "need path of file that needs thumbnailification", "(c) CofenseLabs", "Chip", "Oct 2018", "Black Thumb", "", [ (PF_STRING, "arg0", "ImagePath", "/path/to/image"), ], , blackThumb, menu="<Image>/File/Create") main()
Give it a shot!
Take it a step further and complete the process from the command line!
*I cannot guarantee that this will work for your platform and your installation, you will have to experiment, because it varies from platform to platform, version to version.
*Yes, you have to add “python-fu” to the front of your plug-in name.
/Applications/GIMP.app/Contents/MacOS/GIMP -idf --verbose -b '(python-fu-black-thumb RUN-NONINTERACTIVE "/Users/USERNAME/Desktop/numbered/4.tif")' -b '(gimp-quit 1)'
Insert your best bash-fu here
-Play with Python-Fu Console in conjunction with Procedure Browser to find your commands
-Rewrite as a python function definition
-Adjust to use “*args”, write register function, and appropriate imports
-Relocate as an executable file to GIMP plug-ins directory
-Test from drop-down menus
-Bash script away