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
Read loop variables from text files
-
- Posts: 22
- Joined: 2015-09-27T20:26:53-07:00
- Authentication code: 1151
- 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
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.
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
-
- Posts: 22
- Joined: 2015-09-27T20:26:53-07:00
- Authentication code: 1151
Re: Read loop variables from text files
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...
Thanks again
Miguel
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
Miguel
- 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
Corrected. Thanks
They would need to be piped between them, which is not saving anything really, since it is still two convert commands.
They would need to be piped between them, which is not saving anything really, since it is still two convert commands.
-
- Posts: 22
- Joined: 2015-09-27T20:26:53-07:00
- Authentication code: 1151
Re: Read loop variables from text files
Thanks Fred. I'll keep on piping then
All the best
Miguel
All the best
Miguel