Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created November 30, 2017 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanBuck/b8a57e27cc2901fe7a0b01c7d7ba23e9 to your computer and use it in GitHub Desktop.
Save susanBuck/b8a57e27cc2901fe7a0b01c7d7ba23e9 to your computer and use it in GitHub Desktop.
yearly-photo-print.sh
#!/bin/bash
# NOTES:
# date -d "some date string" +"format" did not work
# Had to install GNU coreutils:
# brew install coreutils
# And then prefix `date` command with a `g` => `gdate`
# The directory of photos you wish to process
source=/Users/Susan/Desktop/photos-to-print/2014-photos-to-print
welcome () {
echo "Source is set to $source. Do you want to continue? (y/n)"
read -${BASH_VERSION+e}r choice
case $choice in
y)
process
;;
n)
echo "Ok, goodbye!";
exit
;;
*)
echo "Unknown command";
;;
esac
}
process () {
count=0
# Define a new directory path with "-processed" appended to it
# This is where the resulting images will be stored
destination="$source-processed"
# Blank slate and create destination directory
rm -rf $destination
mkdir $destination
# Append wildcard to source directory so we can loop through images in this directory
images="$source/*"
# Iterate through images in source directory
for img in $images
do
count=$((count+1))
# Extract just the filename from the full path
filename=$(basename "$img")
# Attempt to get the date from exif date
exifdate=$(identify -format '%[EXIF:DateTime]' "$img")
# If we don't have exif date, use system date
if [[ -z $exifdate ]]; then
echo "Exif date unavailable- used system date"
creationdate=$(gdate -r "$img" +"%Y-%m-%d-%H-%M-%S")
displaydate=$(gdate -r "$img" +"%a %b %d, %Y %I:%M%p")
# Otherwise, use exif data
else
# Split the exif date on colons and load into variables Y, M, D, etc.
read Y M D h m s <<< "${exifdate//[:]/ }"
# Using these variables, create a date matching the format we want
creationdate=$(gdate -d "$Y/$M/$D $h:$m:$s" +"%Y-%m-%d-%H-%M-%S")
displaydate=$(gdate -d "$Y/$M/$D $h:$m:$s" +"%a %b %d, %Y %I:%M%p")
fi
# Make a copy of source image into our destination directory
# Append the creationdate to this copy
newImage="$destination/$creationdate-$filename"
cp "$img" "$newImage"
# Output details
echo -e "\n#$count $newImage"
echo -e "Creation date: $creationdate"
echo -e "Display date: $displaydate"
# Annotate the image
convert "$newImage" \
-gravity South \
-fill white \
-undercolor '#000000' \
-pointsize 100 \
-font Arial \
-annotate +0+30 " $displaydate " "$newImage";
# When testing, abort after x images
# if (( $count == 2 )); then
# exit
# fi
done
}
welcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment