Gimp image optimization with Python plugins

Photo Studio

© Lead Image courtesy of Mike Schilli

© Lead Image courtesy of Mike Schilli

Article from Issue 222/2019
Author(s):

Performing the same Gimp image processing steps again and again is tiresome and error prone. Mike Schilli assigns this task to a Python script via a home grown new menu entry.

Today's cellphone cameras record images in giant formats that are hardly suitable for blogging or sending through narrow data pipes. I tend to scale all of these photos down to 2000x1000 pixels, maybe sharpen them a bit, and perform white balancing on each one. The Gimp image editor has been my tool of choice for many years, but it would be nice if it would help me out by performing these repetitive steps automatically.

In One Fell Swoop

Fortunately, Python DIY scripts can be easily integrated into Gimp. The Ubuntu installation of the gimp package already contains all the ingredients for homemade commands. Listing 1 [1] initializes a new custom plugin for scaling and sharpening a photo. Saved in the ~/.gimp-2.8/plug-ins/ directory with execution rights, Gimp finds the file on startup and adds its new menu entry as requested under Filters | MyStuff | Sharpen and Scale (Figure 1). The entry is then displayed both in Gimp's context and drop-down menus.

Listing 1

sharpscaleplugin.py

 

Figure 1: The menu entry for scaling and sharpening in Gimp, set up with a Python script.

The main plugin script in Listing 1 calls the program logic for image processing in line 21 with sharp_scale(). This function was previously imported in line 3 from the sharpscale.py file (Listing 2) using an import command. Gimp also finds the second script in the plugin directory where the user previously installed it. Since it is only used as a library, no execution rights are required.

Listing 2

sharpscale.py

 

Listing 1 fetches Gimp-specific Python constructs like the register() function for integrating the plugin or constants like PF_IMAGE from the module gimpfu in line 2; scripts located in Gimp's plugin directory find it without additional import paths. The array starting in line 14 of Listing 1 defines two parameters for calling the plugin function, an image in Gimp's internal PF_IMAGE format and its top layer as a drawable; the latter is then used by the function later on for sharpening.

New Menu

If the user clicks on the newly added menu item as shown in Figure 1, the plugin framework jumps to the sharp_scale() function starting in line 6 of Listing 2 and passes the displayed image's descriptor and a reference to the drawable to it. The scale_coords() function computes the dimensions of the reduced image starting in line 30. It takes the current width and height of the image from line 8 and determines whether it is in landscape or portrait format.

It calculates the scaling factor scale by dividing the length of the longest side by the maximum size defined in the constant SIZE_MAX. The scaled image's dimensions are obtained by multiplying the width and height of the original image by the scaling factor in line 39.

Since scaling can take time with large images, and impatient users expect immediate feedback after pushing a button, lines 12 to 14 use gimp_progress_init() to output a status message, which Gimp displays as a progress bar (Figure 2).

Figure 2: Users see a progress bar below the image during the resizing process, which can take some time for larger images.

When compressing the image, Gimp has to combine several pixels into one, which requires smoothing to make the image look natural afterwards. Line 17 therefore sets Lanczos resampling [2] as the interpolation function.

Well-Documented

Gimp documents the parameter values for calling internal functions directly in the application in the dialog box that appears when navigating to Help | Procedure Browser (Figure 3). There you can search for a keyword like scale or sharpen and, after some trial and error, typically find the right internal function and its parameters. If something goes wrong, for example, if the plugin script quits with an error, Gimp writes an error message to the console. It is therefore recommended to start Gimp from the command line during the debug phase so that the messages in the terminal allow conclusions to be drawn about anything that might have gone wrong. After making any changes to the Python code in the plugins, Gimp should be restarted to guarantee that the changes take effect.

Figure 3: The Procedure Browser reveals the parameters of popular Gimp functions.

For retroactive sharpening, the built-in plug_in_sharpen() function grabs the current image in line 24. The desired sharpness value is defined with a value of 10 by the SHARPEN constant in line 4; line 24 passes it on to the Sharpen plugin (a standard Gimp plugin already included). According to the manual, the function requires not only the Gimp image but also its premier layer as a drawable, which, as mentioned above, the plugin registration passed to the sharp_scale() call right from the outset.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Sharpen Images

    How do you sharpen a digital image? A short introduction to the principles and a Perl plugin for GIMP help amateur digital photographers polish their snapshots in a professional way.

  • Perl: Retouching Photos

    In many cases, whole series of digital images need the same kind of modifications, which forces the photo-grapher to repeat the same steps time and time again in GIMP. Have you ever considered retouching in Perl?

  • Gimp Scripting

    Many users turn to GIMP for pictures in the window, but some may not realize GIMP also has scripting capabilities that allow you to automate recurring tasks. The Python scripting language is a useful alternative to the GIMP’s integrated Lisp dialect.

  • Photo Processing with GIMP

    Touch up your digital images with the GIMP image processing tool.

  • GIMP 2.6

    GIMP 2.6 offers exciting improvements for graphics professionals and enthusiasts alike. With this latest release of GIMP, your imagination might just have found its new best friend.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News