0

I am using the convert command to annotate images with text:

convert -pointsize 20 -fill green -draw 'text 270,460 "[TEXT TO ANNOTATE]" ' [INPUT IMAGE NAME].jpg [OUTPUT IMAGE NAME].jpg

I have 100 images and a single .txt file of 100 unique words with each word on their own line. How can I make it so that every single one of the 100 images gets annotated with the text from a single line of the .txt file? To clarify, I want them to be unique - there shouldn't be more than one image annotated with the same text. I'm new to shell scripting and don't even know where to start.

1
  • The design is no robust, add a stray file and the words won't end up on the right images. It would be quite easy to have a file where lines contain both a file name and the word to apply to the image.
    – xenoid
    Jul 25 '20 at 7:58
3

If I understand the request correctly, that could be (POSIX sh syntax):

for file in ./*.[jJ][pP][gG]; do
  IFS= read -r text <&3 || break
  convert -pointsize 20 \
          -fill green \
          -draw "text 270,460 \"$text\"" \
          "$file" "${file%.*}-annotated.${file##*.}"
done 3< /path/to/list.txt

(assuming there's no double quotes in the lines of list.txt).

That processes all the .jpg (case insensitively) files in the current directory in alphabetical order, and for each, one line of text is read from list.txt, the jpg is annotate with the text in that line and saved as original-annotated.jpg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.