Browse Source

Add photo scripts

primary
George Rawlinson 6 years ago
parent
commit
d459ad1657
  1. 31
      bin/bin/count-extdir
  2. 11
      bin/bin/error
  3. 98
      bin/bin/import-photos

31
bin/bin/count-extdir

@ -0,0 +1,31 @@
#!/bin/bash -e
# Name : count-extdir
# Summary : Count number of files with specific extension in a given directory
# Author : George Rawlinson
# Depends : error
# check number of arguments - should be 2
if [ ! $# -eq 2 ]; then
error "Expected 2 arguments: directory & extension"
exit 1
fi
# check if directory exists
if [ ! -d "$1" ]; then
error "Directory does not exist"
exit 1
fi
# recursively find EXT in DIR
find "$1" -type f -iname "*.$2" -printf '\n'| wc -l
# find flags
# type -f - limit to files
# iname - case insensitive
# printf - print new line after each result
# | - pipe results to next command
# wc -l - count number of new lines
# Source : http://unix.stackexchange.com/questions/146760/count-files-in-a-directory-by-extension
exit 0

11
bin/bin/error

@ -0,0 +1,11 @@
#!/bin/bash -e
# Name : error
# Summary : prints strings in red
# Author : George Rawlinson
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
printf "${RED}%s${RESET}\n" "$@"
exit 0

98
bin/bin/import-photos

@ -0,0 +1,98 @@
#!/bin/bash -e
# Name : import-photos
# Summary : Import & sort photos in a given directory
# Author : George Rawlinson
# Depends : error, exiftool, mktemp
# constants
readonly LIBRARY_DIRECTORY="$HOME/Photos" # location of photo library
readonly PHOTO_EXTENSION="cr2" # photo extension (Canon RAW)
readonly PHOTO_LIMIT=9999 # limit on photos to process
readonly DEBUG_FLAG=0 # used to test exiftool
# check dependencies
count=0
for dependency in error exiftool mktemp
do
# list missing dependencies
if [ ! -x "$(command -v $dependency)" ]; then
echo "$dependency missing"
count=$((count+1))
fi
done
# exit script if any dependencies missing
if [ $count -ne 0 ]; then
exit 1
fi
# check number of arguments - should be 1
if [ ! $# -eq 1 ]; then
error "Expected 1 argument: directory"
exit 1
fi
# get source directory
source_directory="$1"
# check if source directory exists
if [ ! -d "$source_directory" ]; then
error "Directory does not exist"
exit 1
fi
# get number of photos
num_photos=$(count-extdir "$source_directory" "$PHOTO_EXTENSION")
# check numbers
if [ "$num_photos" -gt "$PHOTO_LIMIT" ]; then
error "Too many photos to import"
exit 1
elif [ "$num_photos" -eq 0 ]; then
echo "No photos to import"
exit 0
fi
# create temporary directory
echo "Creating temporary directory"
temporary_directory=$(mktemp -d -p /tmp photos.XXXXXX)
# mktemp flags
# u - dry run
# d - make directory
# p - make in given dir
# photos.XXXXXX - create folder titled photos.XXXXXX where X = random characters
# copy photos to temporary directory
echo "Copying $num_photos photos to temporary directory"
cp -R "$source_directory" "$temporary_directory"
# import photos to library
echo "Importing $num_photos photos"
if [ "$DEBUG_FLAG" -eq 1 ]; then
# shellcheck disable=SC2016
exiftool -fileOrder DateTimeOriginal -recurse -verbose '-testname<${DateTimeOriginal}-${SerialNumber}%-.4nc.%e' -d "$LIBRARY_DIRECTORY"/%Y/%Y-%m-%d/%Y%m%d -ext cr2 "$temporary_directory"
else
# shellcheck disable=SC2016
exiftool -fileOrder DateTimeOriginal -recurse -quiet '-FileName<${DateTimeOriginal}-${SerialNumber}%-.4nc.%e' -d "$LIBRARY_DIRECTORY"/%Y/%Y-%m-%d/%Y%m%d -ext cr2 "$temporary_directory"
fi
# exiftool flags
# fileOrder DateTimeOriginal - before moving, sort in chronological order
# recurse + verbose/quiet - recursive search + verbose/quiet output
# testname/FileName - name of the output file (testname used for dry runs)
# DateTimeOriginal - YYYYMMDD (date format set in -d flag below)
# SerialNumber - camera serial number
# -.4nc.%e - sequential 4 digit number starting at 0001, followed by extension
# d - directory to save to
# - DIR/YYYY/YYYY-MM-DD/FileName (set using this flag + testname/FileName)
# ext only match if ext=cr2 (Canon RAW)
# delete temporary directory
echo "Deleting temporary directory"
rm -rf "$temporary_directory"
# done!
echo "All done!"
exit 0
Loading…
Cancel
Save