Last active
December 14, 2015 16:57
-
-
Save sanmiguel/8e6d8746baa94fbbac87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # find ~/Dropbox/Wallpapers -type f -exec ./make_links -x 1000 -y 1000 /Users/sanmiguel/sorted_wallpapers {} \; | |
| # Usage: $0 <basedir> <img> | |
| # creates a link in <basedir>/<width>x<height>/<img> | |
| # e.g. $0 ~/wp_links ~/raw/path/to/foo.jpg | |
| # (where foo.jpg is 1920x1080px) | |
| # -> ln -ns ~/raw/path/to/foo.jpg ~/wp_links/1920x1080/foo.jpg | |
| minwidth=0 | |
| minheight=0 | |
| force="" | |
| blacklist=./blacklist | |
| while getopts fx:X:y:Y:b: c | |
| do | |
| case $c in | |
| f) force=-f ;; | |
| x) minwidth=$OPTARG ;; | |
| y) minheight=$OPTARG ;; | |
| X) maxwidth=$OPTARG ;; | |
| Y) maxheight=$OPTARG ;; | |
| b) blacklist=$OPTARG ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| basedir="$1" | |
| img="$2" | |
| die () { | |
| echo "$@" >&2 | |
| exit 1 | |
| } | |
| if [ -z "$basedir" ] || [ -z "$img" ]; then die "Usage: $0 <basedir> <img>"; fi | |
| if [ ! -f "${img}" ]; then die "Image $img does not exist"; fi | |
| # This is a little naive and doesn't allow duplicated filenames in different folders | |
| if [ $(grep "$(basename ${img})" "$blacklist" | wc -l) -gt 0 ]; then | |
| die "Image $img blacklisted"; | |
| fi | |
| get() { | |
| key="$1" | |
| from="$2" | |
| sips -g $key "$img" 2>&1 | grep $key | grep -v '<nil>' | awk '{print $2}' | |
| } | |
| width=$(get pixelWidth "$img") | |
| height=$(get pixelHeight "$img") | |
| if [ -z "${width}" ] || [ -z "${height}" ]; then die "${img}: no image dimensions"; fi | |
| if [ $width -lt $minwidth ] || [ $height -lt $minheight ]; then die "${img}: too small"; fi | |
| if [ ! -z "${maxwidth}" ] && [ $maxwidth -gt $width ]; then die "${img}: too large"; fi | |
| if [ ! -z "${maxheight}" ] && [ $maxheight -gt $height ]; then die "${img}: too large"; fi | |
| if [ ${width} -gt ${height} ]; then orientation=landscape; else orientation=portrait; fi | |
| #dir=${basedir}/${orientation}/${width}x${height} | |
| dir=${basedir}/${orientation} | |
| link=${dir}/$(basename "$img") | |
| [ ! -d "$dir" ] && mkdir -p $dir | |
| if [ -z "$force" ] && [ -f "${link}" ]; then die "WARNING: File ${link} exists and is a file!" ; fi | |
| ln -ns ${force} "${img}" "${link}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment