Convert a Unix Script to Windows Script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Glynbeard
Posts: 7
Joined: 2011-09-27T06:38:30-07:00
Authentication code: 8675308

Convert a Unix Script to Windows Script

Post 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!
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Convert a Unix Script to Windows Script

Post 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
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert a Unix Script to Windows Script

Post 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.
Glynbeard
Posts: 7
Joined: 2011-09-27T06:38:30-07:00
Authentication code: 8675308

Re: Convert a Unix Script to Windows Script

Post 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!
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Convert a Unix Script to Windows Script

Post 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
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert a Unix Script to Windows Script

Post 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
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Convert a Unix Script to Windows Script

Post 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.
Wolfgang Hugemann
Post Reply