-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert2jpg
executable file
·169 lines (152 loc) · 4.29 KB
/
convert2jpg
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
######################################################################
# Author: [email protected]
# Date: Wed Feb 5 20:19:47 2014
# File: convert2jpg
#
# Usage: convert2jpg [-Sq] [-s N] [-w N] [-c S] image
# Description: A helper script tool wrapping NetPBM utilities
######################################################################
# Set up the defaults
width=1
colour='-color grey'
quiet=""
standardise=' | pnmtojpeg ${quiet}'
commands=""
function print_usage()
{
echo "Usage: $0 [-Sq] [-s N] [-w N] [-c S] image" >&2
echo "Convert images of different types (gif, tiff...) into jpeg" >&2
echo "-S: Enhance the image and make it sharper" >&2
echo "-s: Scale the image to \`N'" >&2
echo "-w: Add a border to the image with width=\`N'" >&2
echo "-c: Add a border to the image with colour=\`S'" >&2
echo "-q: Suppress diagnostic message" >&2
echo "Note that the order of enhance/scale/border options will be" >&2
echo "used as the order to do the corresponding operations" >&2
}
# Mock functions for NetPBM utilities. Note that we use global
# variable `quiet' to avoid parsing the arguments temporarily
# $1 is the input filename (empty means stdin)
# $2 is the output string
# $3 is the diagnostic message
function _netpbm_mock_func()
{
cat $1
echo "$2"
if [[ -z "${quiet}" ]]; then
echo "$3" >&2
fi
}
function pnmnlfilt()
{
_netpbm_mock_func "" "${FUNCNAME} $*" \
"Enhance the image using parameters: $*"
}
function pnmscale()
{
_netpbm_mock_func "" "${FUNCNAME} $*" \
"Scale the image using parameters: $*"
}
function pnmmargin()
{
_netpbm_mock_func "" "${FUNCNAME} $*" \
"Add margin to image using parameters: $*"
}
function giftopnm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert gif file to pnm file"
}
function tgatoppm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert tga file to ppm file"
}
function xpmtoppm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert xpm file to ppm file"
}
function pcxtoppm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert pcx file to ppm file"
}
function tifftopnm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert tiff file to pnm file"
}
function jpegtopnm()
{
local file="$1"
if [[ -n "${quiet}" ]]; then
file="$2"
fi
_netpbm_mock_func "${file}" "${FUNCNAME} $*" \
"Convert jpeg file to pnm file"
}
function pnmtojpeg()
{
_netpbm_mock_func "" "${FUNCNAME} $*" \
"Convert pnm file to jpeg file"
}
while getopts ":Sqs:w:c:" opt; do
case $opt in
S ) sharpness=' | pnmnlfilt -0.7 0.45 ${quiet}'
commands="${commands}${sharpness}" ;;
s ) size=$OPTARG
scale=' | pnmscale -xysize ${size} ${size} ${quiet}'
commands="${commands}${scale}" ;;
w ) width=$OPTARG
if [[ -z "${border}" ]]; then
border=' | pnmmargin ${colour} ${width} ${quiet}'
commands="${commands}${border}"
fi ;;
c ) colour="-color ${OPTARG}"
if [[ -z "${border}" ]]; then
border=' | pnmmargin ${colour} ${width} ${quiet}'
commands="${commands}${border}"
fi ;;
q ) quiet='-quiet' ;;
\?) print_usage
exit 1 ;;
esac
done
shift $(($OPTIND-1))
for filename in "$@"; do
case "${filename}" in
*.gif ) convert='giftopnm' ;;
*.tga ) convert='tgatoppm' ;;
*.xpm ) convert='xpmtoppm' ;;
*.pcx ) convert='pcxtoppm' ;;
*.tif ) convert='tifftopnm' ;;
*.jpg ) convert='jpegtopnm' ;;
* ) echo "$0: Unknown filetype '${filename##*.}'" >&2
exit 1;;
esac
convert="${convert}"' ${quiet}'
outfile="${filename%.*}.new.jpg"
eval "${convert}" "${filename}" "${commands}" "${standardise}" > "${outfile}"
done