Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

BCMATCH


Modifies one image to try to match its brightness, contrast and optionally saturation to that of another image.

Download Script

last modified: December 15, 2018



USAGE: bcmatch [-c colormode] [-s satmode] infile1 infile2 outfile
USAGE: bcmatch [-h or -help]

-c .... colormode ..... colorspace/channel to use to compute
....................... brightness and contrast statistics;
....................... choices are: gray, intensity, luminance,
....................... lightness, brightness, average, magnitude,
....................... rgb; default=gray
-s .... satmode ....... colorspace mode to use to modify saturation;
....................... choices are: HSB or HSL; default is no
....................... change in saturation.
....................... infile2 will be modified to match infile1

PURPOSE: To modify one image to try to match its brightness, contrast and optionally saturation to that of another image.

DESCRIPTION: BCMATCH modifies one image to try to match its brightness and contrast and optionally saturation to that of another image. The second image will be modified to match the first image. Saturation changes only apply to color images, not grayscale ones. The choice of colormode determines what colorspace or channel will be used to measure the mean and standard deviation of the two images. The differences in mean and standard deviation are equated to brightness and contrast changes, which then are converted to intercept and slope of a linear transformation. This linear transformation is then applied to the second image to make the output. If colormode=rgb, then each channel of the second image will be processed idependently. For all other colormodes, a common transformation will be applied to all channels of the second image.

IMPORTANT: As this is a linear transformation, it may not do justice to any non-linear differences between the two images, such as gamma modifications. Furthermore, once information is lost, especially due to too much contrast change, it generally cannot be adequately recovered.

ARGUMENTS:

-c colormode ... COLORMODE is the colorspace/channel to use to compute the brightness and contrast values of the two images. The choices are: gray, intensity, luminance, lightness, brightness, average, magnitude and rgb. Values of gray and intensity are equivalent. If colormode=rgb, then each channel of the image will be processed independently. The default is gray.

Gray or Intensity uses statistics from -colorspace Gray.
Luminance uses statistics from -colorspace Rec709Luma.
Lightness uses statistics from the lightness channel of -colorspace HSL.
Brightness uses statistics from the brightness channel of -colorspace HSB.
Average uses statistics from the first channel of -colorspace OHTA.
Magnitude uses aggregate statistics from all the channels.
RGB uses statistics independently from each channel of -colorspace sRGB/RGB.
See definitions at: http://www.imagemagick.org/script/command-line-options.php#colorspace

Note: generally there are only slight differences between the various non-rgb colormode results. Colormode=rgb can cause color balance shifts.

-s satmode ... SATMODE is the choice of saturation colorspace to use when modifying the saturation. Choices are: HSB and HSL. The default is to make no change in saturation. If used, I recommend HSB. This argument is only applicable when both images are color (not grayscale).

REQUIRES: IM version 6.4.8-9 or higher due to the use of -function polynomial

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Attempt To Increase Brightness And Contrast On Dark And Low Contrast Image

Reference Image (infile1)

Dark, Low Contrast Image (infile2)

Arguments:
-c gray
(default)

Arguments:
-c gray -s hsb

Arguments:
-c gray -s hsl

Arguments:
-c rgb



Attempt To Decrease Brightness And Contrast On Bright And High Contrast Image

Reference Image (infile1)

Dark, Low Contrast Image (infile2)

Arguments:
-c gray
(default)

Arguments:
-c gray -s hsb

Arguments:
-c gray -s hsl

Arguments:
-c rgb



What the script does is as follows:

  • Converts both images to desired colorspace/channel
  • Extracts the saturation channels of both images if
    the images are in color and the user wants to adjust saturation
  • Gets the mean and standard deviation statistics from those images
  • Converts the mean and standard deviation to brightness and
    contrast changes and then those to a linear transformation
    intercept and slope
  • Uses the slope and intercept with -function polynomial to
    transform the second image
  • Uses -modulate to change the saturation if that is desired
    using the change in mean value between the two saturation channels

This is equivalent to the following IM commands for the gray colormode
and no saturation modification.

  • convert $infile1 -colorspace Gray $tmpIA1
  • convert $infile2 -colorspace Gray $tmpIA2
  • m1=`convert $infile1 -format "%[mean]" info:`
  • m2=`convert $infile2 -format "%[mean]" info:`
  • s1=`convert $infile1 -format "%[standard-deviation]" info:`
  • s2=`convert $infile2 -format "%[standard-deviation]" info:`
  • a=`convert xc: -format "%[fx:1+($s1-$s2)/$s1]" info:`
  • bb=`convert xc: -format "%[fx:($m1-$m2)/$m1]" info:`
  • b=`convert xc: -format "%[fx:0.5*($a*$bb-$a+1)]" info:`
  • convert $infile2 -function polynomial "$a,$b" $outfile