Page 1 of 1

Convert a Unix Script to Windows Script

Posted: 2011-10-11T10:17:20-07:00
by Glynbeard
Hello!

I was wondering if there were any users familiar with both the unix and windows side of imagemagick so that I could get this code:

Code: Select all

infile="jyeO6.gif"
inname=`convert $infile -format %t info:`
ww=10
hh=100
hist=`convert $infile -format %c histogram:info:- | sort -k 1 | sed -n 's/^ *\(.*\)$/\1/p'`
echo "hist=$hist"
string=""
colorArr=(`echo "$hist" | sed -n 's/^.*#.* \(.*\)$/\1/p'`)
echo "colors=${colorArr[*]}"
nc=${#colorArr[*]}
nc1=$((nc-1))
echo "nc=$nc"
countArr=(`echo "$hist" | sed -n 's/^\(.*\):.*$/\1/p'`)
echo "counts=${countArr[*]}"
maxcount=${countArr[$nc1]}
echo "maxcount=$maxcount"
string=""
for ((i=0; i<nc; i++)); do
ht=`convert xc: -format "%[fx:round(100*${countArr[i]}/$maxcount)]" info:`	
string="$string -size ${ww}x${ht} xc:${colorArr[i]}"
done
echo "string=$string"
convert -size ${ww}x${hh} $string -background black +append -flip ${inname}_colorhist.gif
Translated into one that can be placed inside of a window .bat file.

I would really appreciate your help!

Thanks a bunch!

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-13T05:42:02-07:00
by whugemann
Well, I could probably do, but you are asking too much: This script is not that easy to understand and you didn't even bother to place one comment line in it.

The easiest appraoch would probably be to install Cygwin and run the script as it is. If you would translate it into a DOS batch file, you would still have to install SED, anyway.

Find some general remarks on the "translation" of Unix scripts at http://www.imagemagick.org/Usage/windows.

Wolfgang Hugemann

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-13T09:50:04-07:00
by fmw42
Wolfgang,

This was my script posted in another topic. It was intended as a demonstration for unix users to use. See viewtopic.php?f=1&t=19538#p76837


infile="jyeO6.gif"

# get first part of name without suffix to use as part of output name
inname=`convert $infile -format %t info:`

# specify desired width of a bin and height of graph
ww=10
hh=100

# get the histogram as a list, sort by count, remove leading zeros
hist=`convert $infile -format %c histogram:info:- | sort -k 1 | sed -n 's/^ *\(.*\)$/\1/p'`
echo "hist=$hist"


# get just the rgb colors from the sorted histogram and put into an array
colorArr=(`echo "$hist" | sed -n 's/^.*#.* \(.*\)$/\1/p'`)
echo "colors=${colorArr[*]}"

# get number of colors in array and number-1 (last entry)
nc=${#colorArr[*]}
nc1=$((nc-1))
echo "nc=$nc"

# get just the counts from the sorted histogram and put into an array
countArr=(`echo "$hist" | sed -n 's/^\(.*\):.*$/\1/p'`)
echo "counts=${countArr[*]}"

# get max count as the last entry in the sorted array
maxcount=${countArr[$nc1]}
echo "maxcount=$maxcount"

# loop over every array value
string=""
for ((i=0; i<nc; i++)); do

# compute the height of the bin from the count/maxcount scaled by the graph height hh
ht=`convert xc: -format "%[fx:round($hh*${countArr}/$maxcount)]" info:`

# concatentate -size and color for all entry in the loop as a string
string="$string -size ${ww}x${ht} xc:${colorArr}"
done
echo "string=$string"

# create each graph bar from the string of sizes and colors, then append them together and flip it over vertically and output as one image.
convert $string -background black +append -flip ${inname}_colorhist.gif


More sophisticated versions and examples are available at my recent unix/bash script, spectrumhist, at the link below.

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-14T08:30:11-07:00
by Glynbeard
Thanks for the commented code fmw42!

I would really prefer to have a windows version of this so I don't have to rely on another program (eg. cygwin) as well to accomplish this task if that's possible.

Thanks again!

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-19T10:37:51-07:00
by whugemann
Fred,

given the context and your comments, I understand what the script does. I doubt that this can be done in a Windows batch file, as the control structures are too limited. The split-up of the text output can be achieved with the FOR command, so one could avoid using sed (which would have to be installed on Windows). Sorting could be done by SORT in a pipe, just like in the Unix shell.

But looping through arrays would have to be transformed into working sequentially through the lines of a text file. This would be a tough and tricky coding job.

Wolfgang Hugemann

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-19T11:55:15-07:00
by fmw42
whugemann wrote:Fred,

given the context and your comments, I understand what the script does. I doubt that this can be done in a Windows batch file, as the control structures are too limited. The split-up of the text output can be achieved with the FOR command, so one could avoid using sed (which would have to be installed on Windows). Sorting could be done by SORT in a pipe, just like in the Unix shell.

But looping through arrays would have to be transformed into working sequentially through the lines of a text file. This would be a tough and tricky coding job.

Wolfgang Hugemann
Not that you need to do it, but would it be easier to use a list rather than array and loop over each list item, such a for each val in $list or some such DOS command? Just curious.

Fred

Re: Convert a Unix Script to Windows Script

Posted: 2011-10-29T02:35:19-07:00
by whugemann
Not that you need to do it, but would it be easier to use a list rather than array and loop over each list item, such a for each val in $list or some such DOS command? Just curious.
Both, enumerations and loops, are performed by way of the FOR command. But there is no such thing as a temporal variable or even an array in a DOS batch file. You can basicly enumerate the items of a list, which is either a text file, the output of a DOS command, the files in a directory or a hard-coded list. So if you want to enumerate items, you have to dump them to a text file first and then process the single lines of this file by means of the FOR command.

If you succeed in "twisting" the process to function that way, DOS batch procession can be very effective, but this often calls for an approach that follows the weird ways of the DOS batch language and does not resemble the approach you would take in a command-driven language. It's a little bit like LISP, which I came to know when programming AutoCAD years ago.