Created
February 14, 2015 17:32
-
-
Save greenkey/9efd3d762fa471eda0af to your computer and use it in GitHub Desktop.
rename images based on EXIF data, move it in proper directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: | |
# $0 path/to/image/or/dir path/to/destination/dir | |
# | |
# the file will be renamed using EXIF data: | |
# YYYYMMDD-image_name | |
# | |
# if the second parameter is specified, a directory structure will be created: | |
# path/to/destination/dir/YYYY/YYYY-MM/image_new_name | |
# otherwise the file will be just renamed | |
rename(){ | |
echo >&2 "-------- rename $@" | |
# input file expansion | |
image_complete_path="$1" | |
image_path=$(dirname "$image_complete_path") | |
image_name=$(basename "$image_complete_path") | |
image_ext="${image_name##*.}" | |
# destination dir | |
dest_dir="$2" # path where to put the image, optional | |
case $image_ext in | |
CR2) | |
echo "Must convert $image_name" | |
#raw2jpg "$image_complete_path" "$image_path/${image_name%.*}.jpg" | |
rename "$(raw2jpg "$image_complete_path")" "$2" | |
return | |
;; | |
esac | |
# check if the file is already dated | |
if [ "1" == $(echo $image_name | grep -c "^[0-9]\{8\}[-_]") ]; then | |
timestamp="${image_name:0:4}:${image_name:4:2}:${image_name:6:2}" | |
if [ "1" == $(echo $image_name | grep -c "^[0-9]\{8\}[-_][0-9]\{6\}") ]; then | |
timestamp="$timestamp:${image_name:9:2}:${image_name:11:2}:${image_name:13:2}" | |
fi | |
image_new_name=$image_name | |
else | |
# get EXIF timestamp | |
timestamp=$(jhead "$image_complete_path" | awk -F" : " '/Date\/Time/ { print $2 }' | tr " " ":") | |
# the new name of file | |
image_new_name=$(echo "$timestamp" | awk -F":" '{ print $1$2$3 }')-$image_name | |
if [ "-$image_name" == "$image_new_name" ]; then | |
echo >&2 "EXIF information not present" | |
return 1 | |
fi | |
if [ ${image_name::8} == ${image_new_name::8} ]; then | |
image_new_name=$image_name | |
fi | |
fi | |
echo "time: $timestamp" | |
echo "new name: $image_new_name" | |
# get the full path for new file | |
if [ "$dest_dir" == "" ]; then | |
image_new_complete_path="$image_path/$image_new_name" | |
else | |
image_new_path=$dest_dir/$(echo "$timestamp" | awk -F":" '{ print $1"/"$1"-"$2 }') | |
mkdir -p $image_new_path | |
image_new_complete_path="$image_new_path/$image_new_name" | |
fi | |
echo "$image_complete_path --> $image_new_complete_path" | |
mv "$image_complete_path" "$image_new_complete_path" | |
jhead -ft "$image_new_complete_path" | |
} | |
raw2jpg() { | |
# input file expansion | |
echo >&2 "-------- raw2jpg $@" | |
file_complete_path="$1" | |
file_path=$(dirname "$file_complete_path") | |
file_name=$(basename "$file_complete_path") | |
file_name="${file_name%.*}" | |
file_ext="${file_name##*.}" | |
if [ "" == "$2" ]; then | |
jpg_file_path="$file_path/$file_name.jpg" | |
echo "$jpg_file_path" | |
else | |
jpg_file_path="$2" | |
fi | |
dcraw -z "$file_complete_path" | |
dcraw -c "$file_complete_path" | cjpeg -quality 80 > "$jpg_file_path" | |
touch -r "$file_complete_path" "$jpg_file_path" | |
jhead -mkexif "$jpg_file_path" > /dev/null | |
jhead -dsft "$jpg_file_path" > /dev/null | |
} | |
# if the first parameter is a file, rename it | |
if [ -f "$1" ]; then | |
rename "$1" "$2" | |
# else if it is a directory, search all files in that dir | |
elif [ -d "$1" ]; then | |
OLDIFS=$IFS | |
IFS=$'\n' | |
for f in $(find "$1" -type f | grep -v "\.!sync") | |
do | |
echo $f | |
rename "$f" "$2" | |
done | |
IFS=$OLDIFS | |
echo "we recommend to delete empty dirs:" | |
echo "find . -type d -empty -delete" | |
# otherwise error | |
else | |
echo >&2 "The input file is neither a file nor a dir" | |
exit 1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment