Changing Photo Color from the Command Line – ImageMagick

ImageMagick

In graphics applications, it is occasionally necessary to perform the same operation on a large number of files.  Instead of performing the operations manually in Photoshop, the computer can often be used to automatically do the work through batch scripting.  By leveraging a command-line program called ImageMagick, most batch photo operations can be scripted for automated processing.

ImageMagick is one of the gems of the Open Source world.  Originally developed by a DuPont computer science researcher in the late 1980’s, the software has grown in scope throughout the years to become one of the most powerful and popular graphics processing libraries.  Ported to a large variety of architectures and platforms, ImageMagick is compatible with most web frameworks and works on the majority of operating systems.  With the ability to process over 200 file formats and perform a large variety of graphics operations, the toolset makes powerful image processing free for public use.

The first step to leveraging ImageMagick to change photo color is to find the HSB (Hue, Saturation, and Brightness) values that will be applied to each image.  This can be done in Photoshop or most major graphics editing software.  Since the color transformation will work on color averages, the average HSB value of both images must be computed to find the target transform.

  1. Open both the original image and the target color in Photoshop
  2. Create a new composite image with the two images (before and after) side-by-side
  3. Using the Point-sample tool, change the Sample Size to the maximum and find the average HSB values of each half of the image
  4. Create “Hue/Saturation” Adjustment Layers over each half of the image.
  5. Starting with the old image, find the values of the HSB transform that will result in the color match to the new image:
    • The Hue adjustment is generally Hnew-Hold
    • The Saturation and Brightness sliders will need to be adjusted manually to find the ideal conversion, however a good starting point is as follows:
      • Brightness starting point: (Bnew/Bold-1)*100
      • Saturation starting point: (Snew/Sold-1)*100

With the final HSB transform values, the following ImageMagick command can apply the transform to each image.

convert "SourceImage" -set option:modulate:colorspace hsb -modulate "B,S,H" "DestinationImage"

It’s important to note that ImageMagick uses different values for the HSB transform than Photoshop.  Be sure to make the following changes to the final HSB values before putting them into the command line:

  1. HImageMagick:  (HPhotoshop)/360+100
  2. SImageMagick:  (SPhotoshop)+100
  3. BImageMagick:  (BPhotoshop)+100

Finally, the convert statement can be embedded within a PowerShell script for automated batch processing of all files within a folder:

Get-ChildItem . -Filter *sourcefile* -recurse | `
Foreach-Object{
$srcimg = $_.FullName
$dstimg = ($srcimg -replace "sourcefile", "targetfile")
& convert "$srcimg" -set option:modulate:colorspace hsb -modulate "90,95,105" "$dstimg"
}

This technique can save considerable time that would be otherwise spent manually altering the photos in Photoshop.  In addition to creating a more uniform effect for all graphics, batch photo processing can also repurpose the image for other applications, and make future color changes that much easier to implement. ImageMagick can be used to Convert, Edit, and Compose Images. For more information see  User Interface Design.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.
Google+

RSS Twitter LinkedIn Facebook Email

Leave a Reply

Your email address will not be published. Required fields are marked *