Perl – Change Contrast / Brightness with Perl + Imagemagick
Summary
Below is a Perl script that can be used to adjust the brightness and contrast for a whole directory of JPG photos automatically. I searched for a while on using the GIMP to automate brightness and contrast changes, but it seemed too complicated for my purposes. I found one GIMP plug-in already written. It installed, but didn’t work under Ubuntu 9.10.
I have found myself needing something like this script for photos that were taken in cloudy, but bright, daylight. Often the photos look more like the original day if I increase the brightness by around 10-40% in the GIMP and slightly increase the contrast. All the photos taken under the same cloudy conditions seem to need about the same adjustment.
Usage
The Perl scripts use the ImageMagick command “mogrify”, which is similar to “convert”, but no new file is created. The original file is permanently altered. You NEED to work on COPIES of your photos and not the originals.
In a Debian-based Linux distribution, like Ubuntu, you would need to install the package “imagemagick”. The easiest way to do this is with the GUI System… Administration… Synaptic Package Manager. Alternatively, on the command line:
me@myhost$ sudo apt-get install imagemagick
The script will prompt you for values of brightness and ContrastBump. The brightness value is given as a percentage. So, the value “100” (no quotes) would give you no change to brightness, “120” is a 20% increase in brightness, and “90” is a 10% decrease. I made up therm ContrastBump because you cannot enter percent values for this, you can only re-apply the “-contrast” flag to the convert or mogrify commands. See the file below.
Procedure for Linux
- Copy the file below and paste into a text editor.
- Make the file executable me@myhost$ chmod u+x FILE_NAME
- Put the script in a directory with ONLY THE FILES YOU WANT TO CHANGE.
- Execute the file on the command line, or double-click it and choose “Run In Terminal”.
- (There is a visual reference to executing a script under linux on the Add Date To Scanned Photo or Negative page.
- Enter your values for %brightness and number of “-contrast” flags when prompted.
- I suggest you test your values on 2 or 3 photos before doing a whole batch.
#!/usr/bin/Perl -w
#
# 2010-02 Kim Briggs. http://kimbriggs.com
#
# ***WORK WITH COPIES AND NOT ORIGINALS***
# Modify the Brightness and Contrast for a directory of JPG images.
# Using command "convert" from "imagemagick" package http://www.imagemagick.org/.
# Currently, "-brightness-contrast" and "-contrast" give "unrecognized option".
# My work-around is below.
#
# The convert command option "-modulate" will change brightness, contrast, hue.
# values are percentages, 100 = No Change, 130 = Increase by 30%.
# The convert command option "-contrast" increases contrast,
# "+contrast" decreases it(!), no values.It can be applied repeatedly.
#
# Using command "mogrify" is like convert, except the original file is changed.
# Get the brightness percent
print "Enter Brightness % (Ex: 130=+30%): ";
chomp($brightness = <>);
# Get self-named ContrastBump
print "Enter the ContrastBump (Choices: 0,1,2,3): ";
chomp($bump = <>);
#Load all jpgs into an array. Remove trailing line feed.
@pix = `ls -1 *.JPG`;
foreach $pix (@pix) {
chomp $pix;
print "Processing photo: ".$pix."\n";
`mogrify -modulate $brightness,100,100 $pix`;
if ($bump == 0) {1 == 1;}
elsif ($bump == 1) {`mogrify -contrast $pix`;}
elsif ($bump == 2) {`mogrify -contrast -contrast $pix`;}
elsif ($bump == 3) {`mogrify -contrast -contrast -contrast $pix`;}
else {print "ContrastBump out of range";}
}
Notes
- Normal photographs will take on a strange look with ContrastBump=3. I doubt you need to go over 2.
- My initial “cloudy day” samples took typically LOWER numbers using ImageMagick than when manually adjusting the brightness and contrast using the GIMP. The two values “120” and “0” gave enough of a change to make the photos seem more true-to-life.