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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.