Created
July 4, 2013 00:43
-
-
Save dhornbein/5924065 to your computer and use it in GitHub Desktop.
This script re-sizes and optionally renames files in the current folder using the Imagemagick convert program.
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
#!/usr/bin/env bash | |
# | |
# By Drew Hornbein @ http://dhornbein.com | |
# | |
# Description: The following script will take image files from | |
# the current folder and run the Imagemagick convert function. | |
# A log.txt file will be created in the new folder | |
# | |
# Dependencies: | |
# imagemagick - we use the convert program | |
# | |
# Options: | |
# | |
# -n file_name | |
# new name: If you wish to rename all files add this flag. | |
# The new name will be numerated like so: new_name_001.jpg | |
# | |
# -s 1200x | |
# size: pass a argument to convert's resize flag. | |
# More info here: http://www.imagemagick.org/script/command-line-options.php#resize | |
# default: 1200x | |
# | |
# -q 90 | |
# quality: pass a number to convert's quality flag | |
# 0-100 higher number is higher quality | |
# default: 90 | |
# | |
# -o 'string' | |
# add option: add additional attributes to the convert function | |
# | |
# -O 'string' | |
# replace options: completely replace all attributes passed to the convert function | |
# with the attributes you input. | |
# | |
# -d ./processed/ | |
# destination: where to send the converted files | |
# default ./processed/ | |
# | |
# -v | |
# verbose: outputs information | |
set -e | |
# check for convert | |
convert -v foo >/dev/null 2>&1 || { echo "imagemagick 'convert' is required to run this script. try: sudo apt-get install imagemagick" >&2; exit 1; } | |
# set help and useage | |
HELPTEXT="help: `basename $0` (-n new file name) (-s resize ex: 1200x, 400x500^) (-q quality 0-100) (-o additional convert/mogrify options ex: \"-o '-extent 400x400'\") (-O capital o, replace parameters of convert/mogrify with input) (-d destination /path/name/) (-v verbose output)" | |
USETEXT="Usage: `basename $0` options (-n new file name) (-s resize) (-q quality) (-o additional convert/mogrify options) (-O replace convert/mogrify options) (-d destination) (-v verbose) -h for help" | |
# set default values | |
NEWNAME=false | |
NEWSIZE="1200x" | |
IMGQUALITY="90" | |
NEWOPTIONS=false | |
DEST=./processed/ | |
if ( ! getopts "n:s:q:o:O:d:vh" opt); then | |
echo $USETEXT | |
fi | |
while getopts ":n:s:q:o:O:d:vh" opt; do | |
case $opt in | |
n)NEWNAME="$OPTARG";; | |
s)NEWSIZE="$OPTARG";; | |
q)IMGQUALITY="$OPTARG";; | |
d)DEST=${OPTARG%/}/;; | |
o)ADDOPTIONS="$OPTARG";; | |
O)NEWOPTIONS="$OPTARG";; | |
v)VERBOSE=true;; | |
h) | |
echo $HELPTEXT | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [[ $NEWOPTIONS == false ]] | |
then | |
echo "(n)ew name: $NEWNAME" | |
echo "(s)ize: $NEWSIZE" | |
echo "(q)uality: $IMGQUALITY" | |
echo "(d)estination: $DEST" | |
fi | |
# prevents a null glob to be read | |
shopt -s nullglob | |
IMGGLOB=(*.{jpg,jpeg,JPG,JPEG,png,PNG,gif,GIF}) | |
FILESLEN=${#IMGGLOB[@]} | |
i=1 #count | |
for INFILE in "${IMGGLOB[@]}" | |
do | |
NUM=$(printf "%0${#FILESLEN}d" $i); | |
EXT=${INFILE##*\.} | |
if [[ $NEWNAME != false ]]; | |
then | |
FILENAME="${NEWNAME}_${NUM}.${EXT,,}" | |
else | |
FILENAME="${INFILE}" | |
fi | |
OUTFILE="$DEST${FILENAME}" | |
# check with the user to make sure everything is okay | |
if [[ -z $OKAY ]]; then | |
# build convert command | |
if [[ $NEWOPTIONS == false ]] | |
then | |
convert="convert '$INFILE' -resize '$NEWSIZE' -gravity center -density 72 -quality '$IMGQUALITY' $ADDOPTIONS $OUTFILE" | |
else | |
convert="convert '$INFILE' $NEWOPTIONS $OUTFILE" | |
fi | |
echo "This command will be run on each file:" | |
echo '$' $convert | |
echo -ne "Process these $FILESLEN file(s)? [Y/n]: " | |
read process | |
if [[ $process == "N" || $process == "n" ]]; then | |
echo "aborting..." | |
exit 42 | |
else | |
OKAY=true | |
mkdir -p "$DEST" | |
echo -e "`date` ----------\n$convert" > "${DEST}log.txt" | |
fi | |
fi | |
if [[ $NEWOPTIONS == false ]] | |
then | |
convert "$INFILE" \ | |
-resize "$NEWSIZE" \ | |
-gravity center \ | |
-density 72 \ | |
-quality "$IMGQUALITY" \ | |
$ADDOPTIONS \ | |
"$OUTFILE" | |
else | |
convert "$INFILE" \ | |
$NEWOPTIONS \ | |
"$OUTFILE" | |
fi | |
IN="$INFILE ($(du -h "$INFILE" | cut -f1))" | |
if [ -f "$OUTFILE" ]; then | |
OUT="$OUTFILE ($(du -h "$OUTFILE" | cut -f1))" | |
fi | |
# build log file | |
echo "$IN ---> ${OUT-FAILED!}" >> "${DEST}log.txt" | |
if [[ $VERBOSE ]]; then | |
echo "$NUM $IN --> ${OUT-FAILED!}" | |
fi | |
(( i += 1 )) | |
done | |
notify-send "Conversion Finished of $i files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment