11 July 2016

The better way to get date and time on your photos

On one of the latest Sony cameras, I switched on a feature that imprints the date the photograph was taken, onto the bottom right corner of the photo.

I was shocked at the result.

Sony's software puts a fat orange date stamp on the photo without even antialiasing it. Looks like we've been transported to the 1970's.

Searching for a better alternative led me to a script written by Terdon that makes use of imagemagick to extract exif information from the picture.

  • You can run it via the linux commandline
  • It's fast 
  • Doesn't mess up your existing image and 
  • Is also configurable

See the difference between Sony's orange date stamp and imagemagick's white one.



Here's what to do at your bash terminal:

sudo apt-get install imagemagick
sudo apt-get install exiv2
  
Then put this script in a file named watermark.sh:

#!/usr/bin/env bash

## This command will find all image files, if you are using other
## extensions, you can add them: -o "*.foo"

find . -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.tif" -o \
 -iname "*.tiff" -o -iname "*.png" |

## Go through the results, saving each as $img
while IFS= read -r img; do
 ## Find will return full paths, so an image in the current
 ## directory will be ./foo.jpg and the first dot screws up
 ## bash's pattern matching. Use basename and dirname to extract
 ## the needed information.

 name=$(basename "$img")
 path=$(dirname "$img")
 ext="${name/#*./}";

 ## Check whether this file has exif data
 if exiv2 "$img" 2>&1 | grep timestamp >/dev/null
 ## If it does, read it and add the water mark
 then
 echo "Processing $img...";
 convert "$img" -gravity SouthEast -pointsize 22 -fill white \
 -annotate +30+30 %[exif:DateTimeOriginal] \
 "$path"/"${name/%.*/.time.$ext}";
 ## If the image has no exif data, use the creation date of the
 ## file. CAREFUL: this is the date on which this particular file
 ## was created and it will often not be the same as the date the
 ## photo was taken. This is probably not the desired behaviour so
 ## I have commented it out. To activate, just remove the # from
 ## the beginning of each line.


 # else
 # date=$(stat "$img" | grep Modify | cut -d ' ' -f 2,3 | cut -d ':' -f1,2)
 # convert "$img" -gravity SouthEast -pointsize 22 -fill white \
 # -annotate +30+30 "$date" \
 # "$path"/"${name/%.*/.time.$ext}";

 fi
done


Change the file to an executable and run it:

chmod +x watermark.sh
./watermark.sh

Run it in the folder where your images are placed.

Works like a charm!

No comments: