Skip to content

Instantly share code, notes, and snippets.

@alanorth
Created May 20, 2016 11:16
Show Gist options
  • Save alanorth/131401dcd39d00e0ce12e1be3ed13256 to your computer and use it in GitHub Desktop.
Save alanorth/131401dcd39d00e0ce12e1be3ed13256 to your computer and use it in GitHub Desktop.
Script to resize large thumbnails before upload into DSpace
#!/usr/bin/env bash
#
# Check pre-generated thumbnails to see if their dimensions are appropriate for
# uploading to DSpace. Some thumbnails come from Flickr or YouTube and are very
# high definition (2000px!), so we resize them to something more appropriate.
#
# Expects to be run while inside a directory of JPEGs. Haven't tested with PNGs.
#
# Alan Orth, 2016-05
# Exit on first error
set -o errexit
readonly IM_CONVERT_BIN=/usr/bin/convert
readonly IM_IDENTIFY_BIN=/usr/bin/identify
readonly THUMBNAIL_MAX_HEIGHT=400
readonly THUMBNAIL_MAX_QUALITY=82
readonly OUTPUT_DIR=processed_thumbnails
main() {
local thumbnail
local thumbnail_height
local thumbnail_quality
mkdir -p $OUTPUT_DIR
for thumbnail in *.jpg
do
echo "Processing $thumbnail"
# get thumbnail's height
thumbnail_height=$($IM_IDENTIFY_BIN -format %H $thumbnail)
# see if its height is larger than we want
if [[ $thumbnail_height -gt $THUMBNAIL_MAX_HEIGHT ]]; then
echo "> Height is too large ($thumbnail_height), resizing"
# check image quality (JPEG specific?)
thumbnail_quality=$($IM_IDENTIFY_BIN -format %H $thumbnail)
# see if its quality is higher than we want
if [[ $thumbnail_quality -gt $THUMBNAIL_MAX_QUALITY ]]; then
echo ">> Quality is too high, will use $THUMBNAIL_MAX_QUALITY"
$IM_CONVERT_BIN "$thumbnail" -resize x${THUMBNAIL_MAX_HEIGHT} -quality $THUMBNAIL_MAX_QUALITY -strip $OUTPUT_DIR/$thumbnail
else
# otherwise ImageMagick uses the image's existing quality setting
$IM_CONVERT_BIN "$thumbnail" -resize x${THUMBNAIL_MAX_HEIGHT} -strip $OUTPUT_DIR/$thumbnail
fi
else
echo '> Height is fine, copying to output directory'
# just copy the existing thumbnail to the output directory
cp $thumbnail $OUTPUT_DIR
fi
done
}
main
# vim: set expandtab:ts=4:sw=4:bs=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment