Score:-1

Automatically adding a number to each image in a directory

cn flag

I want to display a number on top of each image, for all image files in a directory. In other words, each image is modified to contain a box containing a number. How can I automate this?

cn flag
Hi! While its fine to ask a question to post a GOOD answer, both your posts are too short and lack essential detail. (e.g. what does "add a number on top" mean?) Good answers don't consist only of code but they explain what happens and how that solves the problem.
cn flag
Thanks, I improved it based on your comment.
ar flag
Which version of Ubuntu are you using?
cn flag
Ubuntu 21.10. I've updated the answer to work with both new and really old versions.
Score:-2
cn flag

This bash-script does that. It uses imagemagick. Install imagemagick by apt-get install imagemagick on Ubuntu and Debian systems, and a similar yum command on Fedora. The trick is to increase a counter for each image, and use that value when writing the label on each image. All commands used by the script have existed in Ubuntu/Debian since at least 2016, and works with Ubuntu 21.10 as well.

#!/bin/bash
mkdir -p out #creates an output directory called 'out'
i=0
ls -1 *.jpg *.JPEG *.jpeg *.JPG 2>/dev/null|while read image
do
  number=$(printf "%02d" $i)
  convert "$image" -fill black -undercolor '#FFFFFF' -pointsize 25 -gravity northwest -annotate +10+10 "$number" "out/$image"
  ((i=i+1))
done
ar flag
Debian and Fedora as well as versions of Ubuntu that have reached the end of life are off topic here.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.