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.

NEGATIVE2POSITIVE


Converts from a (scanned film) negative image to a positive image.

Download Script

last modified: December 15, 2018



USAGE: negative2positive [-a autostretch] [-l cliplow] [-h cliphigh] [ -c color] [-d density] [-cb colorbalance] [-P1 percent1] [-P2 percent2]
infile outfile

-a .... autostretch .... autostretch image channels; options are:
........................ none (n), together (t), or separate (s);
........................ default=separate
-l .... cliplow ........ clip percent on low end of histogram;
........................ float; 0<=cliplow<=100; default=1
-h .... cliphigh ....... clip percent on high end of histogram;
........................ float; 0<=cliphigh<=100; default=1
-c .... color .......... desired filter color; any valid IM opaque color
........................ value is valid default=yellow
-d .... density ........ density of filter; 0<=integer<=100; 0 is no change;
........................ 100 is most change towards the color;
........................ default=15 for colorbalance != level and
........................ default=0 for colorbalance = level
-cb ... colorbalance ... color balance mode; choices are level, gray,
........................ white, both, auto or none; default=auto
-P1 ... percent ........ percent1 threshold for autolevel processing;
........................ 0<float<00; default=1
-P2 ... percent ........ percent threshold for detecting gray/white
........................ for auto gray and auto white balance; 0<float<100;
........................ default=1

PURPOSE: To convert from a (scanned film) negative image to a positive image.

DESCRIPTION: NEGATIVE2POSITIVE converts from a (scanned film) negative image to a positive image. Options include: autostretching the image channels with or without clipping, color filtering and to color balance via white balance, gray balance, both, automatic or via levels.

ARGUMENTS:

-a autostretch --- autostretch image channels as preprocessing step. The options are: together (t), separate (s) or none (n). The default=separate. Note, for some images with severe channel autostretch, the option separate can cause severe color shifts.

-l cliplow ... CLIPLOW is the cumulative percent at the low end of the histogram whose graylevel will be autostretched to full black. Values are floats between 0 and 100. If cliplow=0, then the autostretch will locate the minimum value in the channel histogram. The default=1.

-h cliphigh ... CLIPHIGH is the cumulative percent at the high end of the histogram whose graylevel will be autostretched to full white. Values are floats between 0 and 100. If cliplow=0, then the autostretch will locate the maximum value in the channel histogram. The default=1.

-c color ... COLOR is the desired filter color. Any valid IM opaque color value is valid. The default=yellow, since yellow is the complement of blue. (After the image is negated, the color is mostly bluish and yellow will remove most of the blue tint).

-d density ... DENSITY of the color filter. Values are 0<=integers<=100. 0 is no color filter processing. 100 is most change towards the selected color. The default=15 for colorbalance != level and the default=0 for colorbalance = level. Density, generally, will not make any significant change when colorbalance = level.

-cb colorbalance ... COLORBALANCE mode. The choices are none, gray, white, both, auto or level. The default=auto. Both will be an equal blend of gray and white. Auto will try to choose the better of gray or white depending upon which one's percent selected pixels' rmse value (from gray or white, respectively is the smallest value. Level uses an autolevel process to get the black point, white point and (gray point converted to) gamma values.

-P1 percent2 ... PERCENT2 threshold for level processing. Values are 0<floats<100; default=1.

-P2 percent2 ... PERCENT2 is the percent threshold for detecting gray/white in the image for auto color balance. Values are 0<floats<100. The default=1.

NOTE: I thank Dr. Guenter Grau for the suggestion to normalize the r,g,b ratios by their average in the color balance part of the code.

REFERENCES AND TUTORIALS:
http://www.colorpilot.com/silver.html
http://www.instructables.com/id/How-to-Convert-Digitized-Film-Negatives-into-Quali/?ALLSTEPS
http://www.computer-darkroom.com/tutorials/tutorial_6_1.htm
https://stefanofoletti.wordpress.com/tag/silverfast/
http://photo.stackexchange.com/questions/23942/does-a-filter-exist-to-color-correct-color-negatives-when-copying-them-with-a-ds
http://howtoresolved.com/tube/NM5XzDYfG60/convert-film-negatives-to-positive-tutorial
https://www.youtube.com/watch?v=B2gLT0SWzpQ
http://petapixel.com/2012/05/18/how-to-scan-film-negatives-with-a-dslr/
https://www.youtube.com/watch?v=iMO50AlGyrw

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


Example 1

Original
(source)

Arguments:
-cb auto or gray



Example 2

Original
(source)

Arguments:
-cb auto or gray



Example 3

Original
(source)

Arguments:
-cb auto or white



Example 4

Original
(source)

Arguments:
-cb auto or gray



Example 5

Original
(source)

Arguments:
-cb auto or white

Arguments:
-cb level



What the script does is as follows:

  • Uses IM compare function to find the best match in the infile
    to a 1x1 pixel referenced colored image
  • Extracts the color of the matched pixel
  • Creates appended, labeled swatches for the refernce color and
    the matched color

This is equivalent to the following IM commands for the case of rgb in the range 0 to 255:

  • data=`compare -metric rmse -fuzz 1000000% $infile \( -size 1x1 xc:$color \) null: 2>&1`
  • coords=`echo "$data" | cut -d\ -f4`
  • xx=`echo "$coords" | cut -d, -f1`
  • yy=`echo "$coords" | cut -d, -f2`
  • rmse=`echo "$data" | sed -n 's/^.*[(]\(.*\)[)].*$/\1/p'`
  • rmse=`convert xc: -format "%[fx:floor(255*$rmse)]" info:`
  • mcolor=`convert ${infile}[1x1+${xx}+${yy}] -format \
    "rgb(%[fx:floor(255*u.r)],%[fx:floor(255*u.g)],%[fx:floor(255*u.b)])" info:`
  • rcolor=`convert -size 1x1 xc:$color -format \
    "rgb(%[fx:floor(255*u.r)],%[fx:floor(255*u.g)],%[fx:floor(255*u.b)])" info:`
  • echo ""
  • echo "RMSE Metric: $rmse"
  • echo "Reference Color: $rcolor"
  • echo "Nearest Image Color: $mcolor"
  • echo ""
  • convert \ \( -background "$rcolor" \
    -fill $labelcolor -font $fontname -pointsize $point \
    -gravity northwest label:"Reference Color:\n\n\n$rcolor" \) \
    \( -background "$mcolor" \
    -fill $labelcolor -font $fontname -pointsize $point \
    -gravity northwest label:"Nearest Color:\n\n\n$mcolor" \) \
    +append $outfile