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
-
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.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.