Read loop variables from text files

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
miguellint
Posts: 22
Joined: 2015-09-27T20:26:53-07:00
Authentication code: 1151

Read loop variables from text files

Post by miguellint »

Hello...

I have two text files, one containg file names and the other containing crop co-ordinates.

FileNames.txt
1.png
2.png
3.png


Coordinates.txt
2888x2330+1135+146
2888x2331+873+160
2889x2331+1007+167



Is there anyway of doing something like the following...

convert $fileName -crop $coordinate +repage $fileName_cropped

...so that I end up with

convert 1.png -crop 2888x2330+1135+146 +repage 1_cropped.png
convert 2.png -crop 2888x2331+873+160 +repage 2_cropped.png
convert 3.png -crop 2889x2331+1007+167 +repage 3_cropped.png


Using Kubuntu 15.10 and IM 6.8.9-9

Thanks very much
Miguel
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Read loop variables from text files

Post by fmw42 »

You will need to write a loop and for each iteration, read each file row and put them into variables, then specify your convert command using the variables for arguments. It should be easy to read each text file and put the values in arrays. Then loop over the number of items, put the array elements into variables in the loop and specify your convert command.

Assuming your image files are in the same directory as your text files, the something like this (untested) should work on unix systems.

Code: Select all

filenameArr=(`cat filenames.txt`)
coordsArr=(`cat coordinates.txt`)
numf=${#filenameArr[*]}
numc=${#coordsArr[*]}
if [ $numf -eq $numc ]; then
  for ((i=0; i<numf; i++)); do
    file="${filenameArr[$i]}"
    filename=`convert -ping "$file" -format "%t" info:`
    coords="${coordsArr[$i]}"
    convert "$file" -crop $coords +repage "${filename}_cropped.jpg"
  done
else
  echo "Number of Files Does not match number of Coordinates"
fi
miguellint
Posts: 22
Joined: 2015-09-27T20:26:53-07:00
Authentication code: 1151

Re: Read loop variables from text files

Post by miguellint »

Hello Fred...

Once again, many thanks. Absolute magic.

A slight typo in line 4 (corrdsArr) which you might like to correct as I'm sure others will want to use your script in future.

---

Can I ask a quick question. Is it possible to chain two (or more) of your scripts together, specifically your Edges and your Multicrop scripts.

Something like...

Code: Select all

for f in *.png
do
file=`convert $f -format "%f" info:`
edges -w 5 multicrop $file $file.png
done
Thanks again
Miguel
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Read loop variables from text files

Post by fmw42 »

Corrected. Thanks

They would need to be piped between them, which is not saving anything really, since it is still two convert commands.
miguellint
Posts: 22
Joined: 2015-09-27T20:26:53-07:00
Authentication code: 1151

Re: Read loop variables from text files

Post by miguellint »

Thanks Fred. I'll keep on piping then :-)

All the best
Miguel
Post Reply