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
-
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.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.