ImageMagick shell script to add caption to directory of images
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Create a shell script that iterates through all photos in a given directory and...

1. Renames photos

Renames each photo, prepending the datetime of when the photo was created.

For example, a photo named DSC_456.jpg that was taken on Nov 27, 2017 at 1:30pm would be renamed to 2017-11-27-23-30-DSC_456.jpg.

2. Overlays a caption on the photos

The caption should...

  • be created using ImageMagick
  • contain the datetime of when the photo was created, following this formatting pattern: Tue Nov 27, 2017 1:30pm
  • positioned bottom center
  • black with a white background, in Helvetica
You need to use the EXIF creation date or the system creation date ?
kerncy 15 days ago
awarded to kerncy

Crowdsource coding tasks.

1 Solution

Winning solution

Hello,

If you want to use the system creation date, here is your script :

for img in *.jpg
do
    creationdate=$(date -r "$img" +"%Y-%m-%d-%H-%M-%S")
    displaydate=$(date -r "$img" +"%a %b %d, %Y %I:%M%p")
    convert "$img" -gravity South -fill black -undercolor '#ffffffff' -pointsize 30 -font Helvetica -annotate +0+30 " $displaydate " "$creationdate""-""$img";
    rm $img
done

be sure to save your images before running the script for first time as the old file will be deleted

EDIT : thanks to poserg for exif information, it takes the exif date if available otherwise it takes the system creation date

for img in *.jpg
do
    exifdate=$(identify -format '%[EXIF:DateTime]' $img)
    if [[ -z $exifdate ]]
    then
        echo -e "exif empty - using system date for $img - \c"
        creationdate=$(date -r "$img" +"%Y-%m-%d-%H-%M-%S")
        displaydate=$(date -r "$img" +"%a %b %d, %Y %I:%M%p")
    else
        echo -e "exif available - using exif date for $img - \c"
        read Y M D h m s <<< ${exifdate//[:]/ }
        creationdate=$(date -d "$Y/$M/$D $h:$m:$s" +"%Y-%m-%d-%H-%M-%S")
        displaydate=$(date -d "$Y/$M/$D $h:$m:$s" +"%a %b %d, %Y %I:%M%p")
    fi
    echo "$creationdate"
    convert "$img" -gravity South -fill black -undercolor '#ffffffff' -pointsize 30 -font Helvetica -annotate +0+30 " $displaydate " "$creationdate""-""$img";
    #rm $img
done
You can get the exif creation date using identify: identify -format '%[EXIF:DateTime]' $img
poserg 15 days ago
Thanks @kerncy. Took your code and ran with it: https://gist.github.com/susanBuck/b8a57e27cc2901fe7a0b01c7d7ba23e9. FYI For posterity - date -d "some date string" +"format" did not work on macOS. Fixed by install GNU coreutils: brew install coreutils
Difranco 13 days ago