Imagemagick, how to print date the photo was taken on the image

Imagemagick 6.9.11-60 on Debian. How to print date & time the photo was taken on the image? (on existing images). I have set this option in camera settings, but it only applies to new photos. The date is on the image medatada. It should be actual date the photo was taken, not the date it was saved on PC harddrive.

Asked By: minto


A lot will vary depending on the images you have and the meta-data they hold, but for example with ImageMagick you can imprint the EXIF date and time from myphoto.jpg with

magick myphoto.jpg -fill black -undercolor white -pointsize 96 -gravity southeast 
 -annotate 0 ' %[exif:datetime] ' output.jpg

If you don’t have the magick command, convert will work just as well.
If you want a non-ISO date format, you can extract the date first with identify and manipulate it with awk or other tools, e.g.

identify -format '%[exif:datetime]' myphoto.jpg |
 awk -F'[ :]' '{print $3":"$2":"$1 " " $4":"$5}'

would change 2023:10:04 09:29:24 to 04:10:2023 09:29.

If your image does not have EXIF data, you might be able to find other time information, e.g. '%[date:create]'. Use identify -verbose on the file to list all the properties.

To calculate a suitable pointsize for the annotation
you can similarly use identify to find the width of the image:

identify -format '%w' myphoto.jpg

See imagemagick for many examples and alternatives.
Here’s a small shell script:

let ps=$(identify -format '%w' input.jpg)/20
# 2023:10:04 09:29:24
datetime=$(identify -format  %[exif:datetime] input.jpg)
if [ -z "$datetime" ]
then    # date:create: 2023-11-11T14:40:21+00:00
        datetime=$(identify -format  %[date:create] input.jpg)
datetime=$(echo "$datetime" | gawk -F'[- :T+]' '{print $3":"$2":"$1 " " $4":"$5}' )
magick input.jpg -fill black  -undercolor white -pointsize "$ps" -gravity southeast 
 -annotate 0 "$datetime" output.jpg

annotated rose

Answered By: meuh