Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Last active August 29, 2015 14:24
Show Gist options
  • Save dmsnell/177d2025082e71048224 to your computer and use it in GitHub Desktop.
Save dmsnell/177d2025082e71048224 to your computer and use it in GitHub Desktop.
Automatically generate web versions of high quality TIFF images placed in a given directory (with fswatch)
# Generates varying exports of a TIFF image
# for different web uses: one high quality
# image for Flickr and two scaled-down lower-quality
# versions for posting to blogs, Twitter, etc...
#
# Only operates on TIFF images because I wanted
# to preserve as much quality as I could until the
# end and also because I don't want to allow a loop
# on the output of this file triggering a new run.
#
# Intended to be used with fswatch (https://github.com/emcrisostomo/fswatch)
# This will automatically generate web versions of any TIFF images
# saved into the running directory and delete the high quality
# source TIFFs.
#
# Example
# fswatch -0 -e Flickr/ -e Blog-half/ -e Blog-1200x/ . | xargs -0 -n1 -I {} ./generateWebVersions.sh {}
#
# Touch `clean` to wipe out the generated images, useful for
# starting fresh on an export to another album
#
# Author: Dennis Snell ([email protected])
FILE=$(basename "$1")
NAME=$(basename "$FILE" .tif)
TYPE=$(file --brief --mime-type "$FILE")
if ! [ -f "$FILE" ]; then
exit 1
fi
if [[ "clean" == "$FILE" ]]; then
rm -rf Flickr
rm -rf Blog-half
rm -rf Blog-1200x
rm clean
exit 0
fi
if ! [[ "image/tiff" == $TYPE ]]; then
exit 1
fi
if ! [ -d "Flickr" ]; then
mkdir Flickr
fi
if ! [ -d "Blog-half" ]; then
mkdir Blog-half
fi
if ! [ -d "Blog-1200x" ]; then
mkdir Blog-1200x
fi
echo "Optimizing ${1}"
function t2j () {
local QUALITY=$1; shift
local BASE_DIR=$1; shift
convert "${FILE}" -quality $QUALITY $@ "${BASE_DIR}/${NAME}.jpg" 2>/dev/null
}
function optimize () {
local QUALITY=$1; shift
local BASE_DIR=$1; shift
jpegoptim "${BASE_DIR}/${NAME}.jpg" -m$QUALITY $@
}
# Flickr
t2j 90 Flickr
optimize 90 Flickr --all-progressive
# Half-width images
t2j 98 Blog-half -resize 50% -filter box
optimize 87 Blog-half -s --all-progressive
# 1200 pixel wide images
t2j 98 Blog-1200x -resize 1200x
optimize 87 Blog-1200x -s --all-progressive
rm "$FILE"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment