Gimp image optimization with Python plugins
Photo Studio
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
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).
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.
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.