#!/bin/bash # # im_profile [option] input_image output_profile_image # # Plot a profile of a horizontal grayscale image given. The center row # of pixels will be extracted if multiple rows are present. # Including a small copy of the input gradient image along the X axis # of the profile graph. # # Options # -v Generate a vertical profile through the image. # -s generate a 150x100 thummbnail image (default 300x200 pixels) # -g Draw the profile in green instead of red # -b Draw the profile in blue instead of red # # See Also # im_histogram draw methematical expression as two images # im_graph draw a histogram mapping function of the given IM options # #### # # WARNING: Input arguments are NOT tested for correctness. # This script represents a security risk if used ONLINE. # I accept no responsiblity for misuse. Use at own risk. # # Anthony Thyssen, 5 July 2008 # PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path PROGDIR=`dirname $PROGNAME` # extract directory of program PROGNAME=`basename $PROGNAME` # base name of program Usage() { # output the script comments as docs echo >&2 "$PROGNAME:" "$@" sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 3s/^/Usage: /; 2,$ p' \ "$PROGDIR/$PROGNAME" exit 10; } Error() { # output the script comments as docs echo >&2 "$PROGNAME:" "$@" exit 10; } COLOR=red while [ $# -gt 0 ]; do case "$1" in --help|--doc*) Usage ;; # Documentation -v) VERTICAL=true ;; # generate a vertical profile though the image. -s) SMALLER=true ;; # smaller 'thumbnail' profile -g) COLOR=green ;; # graph color green -b) COLOR=blue ;; # graph color blue -) break ;; # STDIN, end of user options --) shift; break ;; # end of user options -*) Usage "Unknown option \"$1\"" ;; *) break ;; # end of user options esac shift # next option done [ $# -eq 0 ] && Usage "Missing input_image" [ $# -gt 2 ] && Usage "Too many arguments" # Temporary working images (with auto-clean-up on exit) input="/tmp/im_profile_$$.miff" trap "rm -f $input; exit 0" 0 trap "rm -f $input; exit 1" 1 2 3 15 # Read in and extract the row (column) to be profiled. [ "$VERTICAL" ] && VERTICAL="-transpose" convert -regard-warnings "$1" +repage $VERTICAL \ -gravity center -crop 0x1+0+0 +repage \ +gravity $input 2>/dev/null [ "$?" -eq 0 ] || Error "Invalid Input Image \"$1\"" # Warning: as of IM v6.5.3-9 'gravity' meta-data is saved in MIFF: file format # As such we need to turn it off before saving. output="$2" # output image filename (or special format) [ $# -ne 2 ] && output="show:" # if not given, display it direct # --------------------------------------------- [ -f ./generate_options ] && source ./generate_options [ -f ../generate_options ] && source ../generate_options [ -f ../../generate_options ] && source ../../generate_options [ "$page_bg_color" ] || page_bg_color=LightSteelBlue # width graph_size gnuplot_size final_image_size #size=300 rsize=298x15 gsize=340,160 # 300x150 image #size=300 rsize=298x15 gsize=340,210 # 300x200 image #size=150 rsize=148x10 gsize=190,100 # 150x85 image #size=150 rsize=148x10 gsize=190,115 # 150x100 image size=300 rsize=298x15 gsize=340,210 # 300x200 image if [ "$SMALLER" ]; then size=150 rsize=148x10 gsize=190,115 # 150x100 image fi resize="-resize ${rsize}!" if [ `convert $input -format %w info:` -eq $size ]; then # Input image size is a perfect fit, # Don't simply resize the image which can cause distortions. # especially in cases close to the 'nyquest frequency' # all we need to remove the edges so the black border can replace them. resize="-shave 1x0 $resize" fi # Gnuplot of a Level function #im_func='-sigmoidal-contrast 8x50%' #name=x: { echo "set terminal png size $gsize" echo 'unset key' echo 'set format ""' [ "$COLOR" = 'red' ] && style=1 [ "$COLOR" = 'green' ] && style=2 [ "$COLOR" = 'blue' ] && style=3 [ $size -eq 150 ] && echo 'set xtics ( 38, 75, 112 ) scale 2' [ $size -eq 300 ] && echo 'set xtics ( 75, 150, 225 ) scale 2' echo 'set ytics ( 0.25, 0.5, 0.75 ) scale 2' #echo 'set xtics ( 30, 60, 90 ) scale 4' #echo -n 'plot [0:119][0:1] "-" binary format="%ushort" endian=swap' #echo ' array=120 using ($1/65535) with lines' #convert -size 1x120 gradient: -rotate 90 $im_func -depth 16 gray:- echo 'plot [0:'${size}'][0:1] "-" using 0:($3/65535) with lines '"$style" convert "$input" -depth 16 -resize ${size}x1\! txt:- |\ tail -n+2 | tr -sc '0-9\n' ' ' } | gnuplot | \ convert "$input" $resize \ -bordercolor black -border 1x0 \ -size ${size}x1 xc:black -append \ \( - -trim +repage \) +swap -append \ "$output" #show: ; exit # broken in IM v6.5.4-6 -- fixed in nest release #-bordercolor black -border 1x1 +gravity -chop 0x1 \ # broken in IM v6.5.4-6 -- fixed in nest release #-bordercolor black -border 1x0 \ #-background black -gravity south -splice 0x1 +gravity \ # works in that version # -bordercolor black -border 1x0 \ # -size ${size}x1 xc:black -append \ [ -f "$output" ] && chmod 644 "$output"