-
Notifications
You must be signed in to change notification settings - Fork 23
/
imageresize
executable file
·95 lines (79 loc) · 2.42 KB
/
imageresize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
#
# imagecrop : Resize and crop image to specified size
#
# Version 0.2 (2010/07/21)
# (c) 2010 Mathieu Comandon
# Licensed under the terms of the GPL Version 3
#
set -e
CONVERT=convert
if [ -z $(which $CONVERT) ] ; then
echo "imagemagick is not installed"
exit 2
fi
usage()
{
cat << EOF
Usage: $0
Resize an image to a given maximum size
OPTIONS:
-d, --dir=PATH Input directory containing the images to convert
-s, --size=SIZE Maximum size of the output image
-r, --keep-ratio Keep ratio, do not make a square image
-v, --verbose Verbose mode
-h, --help Prints this message
EOF
}
#Parse arguments
if [ "$#" -eq 0 ] ; then
usage
exit 2
fi
PARAMS=`getopt -n $0 -o d:s:rvh --long dir:,size:,keep-ratio,verbose,help -- "$@"`
eval set -- "$PARAMS"
while true ; do
case "$1" in
-d|--dir) basedir=$2; shift 2 ;;
-s|--size) size=$2; shift 2 ;;
-h|--help) usage ; exit 1 ;;
-v|--verbose) verbose=1 ; shift ;;
-r|--keep-ratio) keep_ratio=1; shift ;;
--) shift ; break ;;
*) usage ; exit 2 ;;
esac
done
#Error checking
error_state=0;
if [ -z "$basedir" ] ; then
echo "No input dir specified !"
error_state=1
fi
if [ -z "$size" ] ; then
echo "No size specified !"
error_state=1
fi
if [ "$error_state" = 1 ] ; then
echo "There are errors in your arguments, exiting."
exit 2
fi
ext='.jpg'
for sourcefile in $(ls $basedir) ; do
filename=${sourcefile%\.*}
destfile=${filename}_${size}${ext}
if [ "$verbose" = 1 ] ; then
echo "Converting "$basedir$sourcefile
fi
if [ -z $keep_ratio ] ; then
square_image_params="-gravity center -crop ${size}x${size}+0+0 +repage"
geometry_modifier="^"
fi
$CONVERT -thumbnail "${size}x${size}${geometry_modifier}" $square_image_params -sharpen 0.25x0.25 -quality 95 -format jpg $basedir$sourcefile $basedir$destfile
done
# TODO
# Adding Annotation To Cropped Thumbnails Using ImageMagick
# http://return-true.com/2009/03/adding-annotation-to-cropped-thumbnails-using-imagemagick/
# Cropping The Thumbnails
# $CONVERT -size 120x108 'path/to/file.jpg' -thumbnail 120x108^ -gravity center -extent 120x108 'path/to/output.jpg'
# Adding The Annotation
# $CONVERT -size 120x108 'path/to/file.jpg' -thumbnail 120x108^ -gravity center -extent 120x108 -gravity south -background black -fill '#cccccc' -font '/path/to/font.ttf' -pointsize 8 -splice 0x12 -annotate +0+1 '%[width]x%[height] %[size]' 'path/to/output.jpg'