Putting an oldtime tint in your digital images
Masked Ball
In GIMP, masks select parts of an image, just as you would with the selection tool to create a rectangular or circular selection in an image. GIMP draws "marching ants" around the selection when you do so and stores the selection information as a black-and-white image in which the white regions represent the selected parts of the image and the dark pixels represent unselected areas.
If the user draws a rectangle at the center of the image, the corresponding mask is a black image with a white rectangle at its center. And Masks can go even further: In contrast to mouse-based selections, masks can also define grayscales, in which the areas of the image are selected "softer" or "harder."
These grayscale images can be put to intelligent use as filters. Instead of painstaking manual selections, the user defines a mask image and GIMP automatically selects the parts of the original image that have corresponding light pixels in the mask image.
To define how the layer's pixels are to be applied to the image, GIMP lets you define a layer mask for any layer. The white areas of this grayscale mask make the layer pixels 100 percent opaque, whereas the black pixels in the mask make the layer 100 percent transparent; that is, they disable this part of the layer. For gray areas, GIMP applies the layer value to a certain degree, reflecting the value in the mask.
Figure 6 shows the Layers dialog with a big black circle that I added to the white background of the layer mask in the sepia layer as a test; the layer's overlay mode is set to "Normal." As you can see in Figure 7, GIMP does not change the original image in the black part of the mask but applies sepia to the white parts and ignores the image in the process. This approach is definitely not the right one, but a grayscale mask will get me closer to my goal.
Original as a Mask
What does the mask need to look like to apply a realistic looking sepia tint – that is, to open up the sepia floodgates for the darker parts of the original and leave them shut in the lighter areas? If a pixel in the original layer is black, the mask at this point has to be white, and the sepia tint will be applied 100 percent. The opposite also applies: If the original layer is white, the mask is black, and the sepia layer is not applied to the original layer at all.
For grayscales, the mask is somewhere in between. Black-to-white and white-to-black? The solution should now be obvious: The mask simply needs to invert the grayscale image of the original layer!
For this, edit_copy() in line 82 copies the image in the original layer into GIMP's internal cut-and-paste buffer, and line 85 drops this content on top of the previously created $layer_mask. This provides a reference to a floating section, which is inverted by line 90 and finally anchored in the layer mask by line 91.
To do this in GIMP by hand, you would click the original layer in the layer dialog, then change to the image window, press Select-All, then press Ctrl+C to copy the image content to the cut-paste buffer. This procedure is followed by a mouse click on the layer mask in the sepia layer (the second thumbnail in the layer line) and a return to the image window, where you need to select Paste to insert. This creates a floating section in the Layers dialog, which is fixed in place by clicking the anchor at the bottom. Finally, you would need to invert the colors in the layer mask by selecting Colors | Invert; after doing so, the Layers dialog would look similar to Figure 8.
The final step is to flatten() (i.e. merge the two layers to create a single active one). This changes the order of the active layers and the script thus needs to call the get_active_layer() method (line 97) to obtain a reference to the remaining layer.
The results, with a nice coating of sepia, are shown in Figure 9, and for a more dramatic effect, as shown in Figure 10, the curves_spline function darkens the dark tones slightly, without touching the lighter ones. The six coordinates passed in to the function in line 102 define a graph, as shown in Figure 11. It manipulates the image as if the user had run GIMP's Curves against it. A linear curve in the Curves dialog leaves the image unchanged, whereas data points below the straight line darken all image pixels carrying that brightness value.
The gimp_file_save() function saves the results in a new file, which line 104 creates by appending -sepia to the original name.
Installation
To install the required Perl modules, Gimp and Gimp::Fu, on Ubuntu Hardy Heron, enter:
sudo apt-get install libgimp-perl
Older Ubuntu versions have a bug, but in a previous article [5], I told you how to work around it. The Log4perl module (available from CPAN or in the liblog-log4perl-perl package with your Ubuntu distribution) shows the progress of the image conversion at the command line. If you prefer a less verbose script, you can comment out the call to easy_init() in line 10 of the script.
The documentation for GIMP's Perl interface isn't very detailed, but GIMP has an excellent procedure browser (Figure 12) that you can call via Xtns | Procedure Browser.
After doing so, type the part of the function name that you have already guessed, such as "load," "save," or "layer," and the procedure browser will return a list of available API functions with precisely documented parameters and return values. It is thus typically possible to find an API function for any action in GIMP's point-and-click user interface, empowering the user to automate steps in scripts instead of repeating them by hand.
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/102/Perl
- "Luminosity Masks and Sepia Toning" by Doug Nelson, RetouchPRO, May 2001, http://www.retouchpro.com/tutorials/lum-mask-sepia.html
- Sepia: http://en.wikipedia.org/wiki/Sepia_tone
- "Sepia Toning" by Eric R. Jeschke, 2002, http://www.gimp.org/tutorials/Sepia_Toning
- "Color Play" by Michael Schilli, Linux Magazine, September 2008, http://www.linux-magazine.com/issues/2008/94/color_play
- Bunks, Carey. Grokking the GIMP. New Riders Publishing, 2000
« Previous 1 2
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
-
2024 Open Source Professionals Job Survey Now Open
Share your expectations regarding open source jobs.
-
Arch Linux 2023.12.01 Released with a Much-Improved Installer
If you've ever wanted to install Arch Linux, now is your time. With the latest release, the archinstall script vastly simplifies the process.
-
Zorin OS 17 Beta Available for Testing
The upcoming version of Zorin OS includes plenty of improvements to take your PC to a whole new level of user-friendliness.
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.