Retouching with Perl
Color Play
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?
Before posting snapshots I've taken with my digital camera to the web, I used to go through the same old steps with GIMP. First, I'd reduce the image size to a width of 1,000 pixels because the image size my Nikon D70 gives me is just too much for the web and uploading the images takes too long. Also, to improve the contrast, correct any color casting, and make images tack-sharp with GIMP's Sharpen function, I typically select a value of 20.
As you might know, GIMP has a convenient scripting interface that lets users automate recurring tasks, and you can even run it at the command line without launching the GUI. GIMP developers have made some fairly significant changes to the whole API and nothing works the way it used to. Fortunately, GIMP documents the functions completely and thoroughly in the Xtns | Procedure Browser menu.
The standard interface is implemented in the rocket scientist's favorite programming language, Scheme.
Perl Module
Thank goodness there is a Perl module. I needed a couple of tricks to install it on Ubuntu 7.10, and I'll explain the steps later. After the install, you can run the picfix script (Listing 1) at the command line by typing picfix file.jpg. Under the hood, the script calls GIMP (without the GUI) and then invokes register() to register the picfix() function.
Strangely, the GIMP interface insists on scripts that run at the command line, creating a menu entry, and picfix has no option but to comply. Line 49 of the script calls the main() function that branches off to GIMP and returns after completing its chores, and exit makes sure that the script gives the return code from main() before quitting.
Figure 1 shows the original image, a picture taken from my balcony in San Francisco with the early evening sun lighting up the city nicely. Despite this, I still wasn't happy with the colors in the original.
Listing 1
picfix
Picfix
Running picfix gave me the results you can see in Figure 2. The contrast is vastly improved and the colors are far more realistic.
The picfix() function defined in lines 52ff., which GIMP executes as a callback, runs the gimp_file_load() function that loads the digital image from a given file on disk. A call to image_scale() (line 134) scales the image to a width of 1,000 pixels.
The gimp_levels_stretch() function (line 74) then simulates the Auto button in the GIMP Levels dialog and maximizes the contrast by distributing the color values used in the image across the whole of the available bandwidth.
The plug_in_sharpen() method (line 90) with a parameter of 20 then improves the focus, then gimp_file_save() (line 97) saves the file as file-1.jpg using the original format. Whether the image is a JPEG or PNG makes no difference; internally, the load/save methods hand control over to the GIMP routines for the format in question.
Loading the supporting modules by calling use Gimp qw(:auto) and use Gimp::Fu has the practical side effect of loading all of GIMP's functions into the script's namespace. Programmers will be happy to hear that the :auto tag also includes GIMP constants, such as RUN_NONINTERACTIVE.
Options
Image rescaling defaults to a width of 1,000 pixels; however, the -s (size) command-line option supports arbitrary values. A command line of picfix -s 500 file.jpg reduces the maximum image width to 500 pixels, for example, with the maximum size relating to the width in landscape images, but to the height in portrait format. The scale_image_down() subroutine in line 109ff. provides the If/Else logic for this.
If you prefer to check the results of your scripted actions on the image, you can set the -x (for X Window) option to view the image in the viewer set as a default in line 13 (in this case, Eye of Gnome, eog).
To disable the Autolevel function, which does not always return satisfactory results, set the -l 0 option.
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.