My goal is to automate the annotation feature in convert (Imagemagick) for myself. Running the script in a folder with some images: I try to read a text file with the annotation titles, each on a new line. Then read the file into an array. (maybe there is a better way?)
I am not able to understand how to add each value of the array and loop through all the images in the folder.
Here is the script so far:
#!/usr/bin/bash
## content of file.txt
sample 1
sample 2
sample 3
sample 4
sample 5
sample 6
sample 7
sample 8
sample 9
## Read from a file into an array, print the array
array=()
# Read the file in parameter and fill the array named "array"
getArray() {
i=0
while read line # Read a line
do
array[i]=$line # Put it into the array
i=$(($i + 1))
done < $1
}
getArray "file.txt"
## Here my problems start
for f in *.jpg; do fn=${f%.*};
for e in "${array[@]}";do
convert ${fn}.jpg -fill white -gravity South -pointsize 32 -annotate +0+5 "$e" ${fn}_annotated.jpg ;done
done
Here is a solution:
# Store the whole file in an array
readarray array < "file.txt" || exit 1
i=0
for f in *.jpg ; do
fn=${f%.*}
title=${array[i++]} # in array[] bash performs arithmetic expansion
convert "$fn.jpg" -fill white -gravity South -pointsize 32 \
-annotate +0+5 "$title" "${fn}_annotated.jpg"
done