Skip to content

Instantly share code, notes, and snippets.

@bhalash
Created December 19, 2015 23:12
Show Gist options
  • Select an option

  • Save bhalash/34141d7a7666dcce1cf6 to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/34141d7a7666dcce1cf6 to your computer and use it in GitHub Desktop.
Image Import Sorter
#!/usr/bin/env bash
# Blame Mark ([email protected]) for this.
function trim_spaces() {
# Safely remove spaces in the file name.
new_name=$(sed 's/[[:space:]]/_/g' <<< "${1}")
mv "$1" $new_name && echo $new_name
}
function test_aspect_ratio() {
# Test whether the aspect ratio of image $1 == $2:1
x=$(identify -format '%w' $1)
y=$(identify -format '%h' $1)
if [[ $file =~ \.mov$ ]]; then
echo 1
return 1
fi
if [[ $x -ge $y ]]; then
x=$(bc <<< "scale=0; ${x} * ${2}")
else
y=$(bc <<< "scale=0; ${y} * ${2}")
fi
if [[ $x -eq $y ]]; then
echo 0
else
echo 1
fi
}
function make_dated_directory() {
# Creates a dated folder and moves the image into it.
directory_name=${1:2:10}
if [[ ! -d $directory_name ]]; then
mkdir $directory_name
fi
mv $1 $directory_name
}
uploads_folder=$HOME'/Dropbox/Camera Uploads/'
instagram_folder=$HOME'/Dropbox/Photos/Instagram/'
IFS='
'
if [[ $UID == 0 ]]; then
# Y U RUN AS ROOT????
exit 1
elif [[ ! -d "${uploads_folder}" ]] || [[ ! -d "${instagram_folder}" ]]; then
exit 2
fi
cd "$uploads_folder"
# Delete .gif and .png files added from my phone.
find -E . -maxdepth 1 -type f -regex "^.*\.(gif|png)$" -exec rm "{}" \;
for file in $(find -E . -maxdepth 1 -type f -regex "^.*[[:space:]].+(jpg|mov)$"); do
file=$(trim_spaces "$file")
if [[ $(test_aspect_ratio $file 1) -eq 0 ]]; then
mv $file "${instagram_folder}"
else
make_dated_directory $file
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment