Skip to content

Instantly share code, notes, and snippets.

@unabridgedxcrpt
Created December 11, 2015 15:14
Show Gist options
  • Save unabridgedxcrpt/c2d8384de245534504cc to your computer and use it in GitHub Desktop.
Save unabridgedxcrpt/c2d8384de245534504cc to your computer and use it in GitHub Desktop.
Grab the EXIF date from original date time field and overlay it on a photo with white text with black stroke font.
#!/bin/sh
#Credit: http://dptnt.com/2009/08/add-date-time-stamp-to-jpeg-photos-using-free-software-mac-linux-nix-edition/
# I added:
# 1. formatting the EXIF date to use the month name and remove the seconds from the time display
# 2. outline the text for lighter backgrounds and better contrast in more images.
#Dependencies: imagemagick, exiftool
# Change the font variable to point to your font location
##font="~/Library/Fonts/digital-7 (mono).ttf"
## digital 7 for to make date time like the *olden days*: http://www.dafont.com/digital-7.font
font="/Library/Fonts/Arial Black.ttf"
## really big system font for sight-impaired relatives :)
if [ $# -eq 0 ]
then
cat << _EOF_
USAGE: $0 file1 file2 ..., or
$0 *.jpg, or
$0 dir/*.jpg
...
_EOF_
exit
fi
while [ "$1" != "" ]; do
# Skip directories
if [ -d "$1" ]; then
shift
continue
fi
# Skip already converted files (may get overwritten)
if [[ $1 == *_DT* ]]
then
echo "------ Skipping: $1"
shift
continue
fi
# Work out a new file name by adding "_DT" before file extension
file=$1
echo "###### Working on file: $file"
filename=${file%.*}
extension=${file##*.}
output=${filename}_DT.${extension}
# Get the file dimension
dim=$(identify -format "%w %h" "$file")
width=${dim%% *}
height=${dim#* }
# Decide the font size automatically, 30 is usually good, but I needed it larger
if [ $width -ge $height ]
then
pointsize=$(($width/22))
else
pointsize=$(($height/22))
fi
echo " Width: $width, Height: $height. Using pointsize: $pointsize"
exifdate=`exiftool -d " %Y %b %e %I:%M %p " -DateTimeOriginal -S -s "${file}"`
echo "------ $exifdate" #sanity check in processing
# The real deal here
convert "$file" -gravity SouthEast -font "$font" -pointsize $pointsize -fill white -stroke black -strokewidth 4 -annotate +$pointsize+$pointsize "$exifdate" -stroke none -annotate +$pointsize+$pointsize "$exifdate" "$output"
shift
done
exit 0
@danielhoherd
Copy link

Here's a slightly modified version that:

  • passes shellcheck
  • uses two spaces
  • re-orders and tweaks some logic
  • reformats a few lines
  • reduces the font size.

This worked well on photos with 1600px long side.

#!/usr/bin/env bash
# Credit:
# - http://dptnt.com/2009/08/add-date-time-stamp-to-jpeg-photos-using-free-software-mac-linux-nix-edition/
# - https://gist.github.com/unabridgedxcrpt/c2d8384de245534504cc

font="/Library/Fonts/Arial Black.ttf"

if [ $# -eq 0 ] ; then
  cat << _EOF_

USAGE: $0 file1 file2 ..., or
       $0 *.jpg, or
       $0 dir/*.jpg
       ...

_EOF_
  exit
fi

while [ "$1" != "" ]; do
  # Skip directories
  if [ -d "$1" ]; then
    shift
    continue
  fi

  # Work out a new file name by adding "_DT" before file extension
  file=$1
  echo "######  Working on file: $file"
  filename=${file%.*}
  extension=${file##*.}
  slug="_DT"
  output=${filename}${slug}.${extension}

  # Skip already converted files
  if [[ "$1" == *${slug}* ]]
  then
    echo "------  Skipping: $1"
    shift
    continue
  fi

  # Get the file dimension
  dim=$(identify -format "%w %h" "$file")
  width=${dim%% *}
  height=${dim#* }

  # Decide the font size automatically, 30 is usually good, but I needed it larger
  if [ "$width" -ge "$height" ]
  then
    pointsize=$((width/50))
  else
    pointsize=$((height/50))
  fi
  exifdate=$(exiftool -d "%F %T%z" -DateTimeOriginal -S -s "${file}")

  echo "  Width: $width, Height: $height. Using pointsize: $pointsize"
  echo "------  $exifdate"      #sanity check in processing

  # The real deal here
  convert "$file" \
    -gravity SouthEast \
    -font "$font" \
    -pointsize $pointsize \
    -fill white \
    -stroke black \
    -strokewidth 4 \
    -annotate +$pointsize+$pointsize "$exifdate" \
    -stroke none \
    -annotate +$pointsize+$pointsize "$exifdate" "$output"

  shift
done

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment