-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimresize.py
More file actions
257 lines (202 loc) · 7.75 KB
/
imresize.py
File metadata and controls
257 lines (202 loc) · 7.75 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
"""``imresize`` is an utility which resizes and/or converts a set of
images.
Run with "--help" for the syntax.
No PEP8, no argparse, etc. This is an old script. ;)
Published under the 3-clause BSD license, a copy of which is included
below.
--- 8< ---
Copyright (c) Damien Baty
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Poulda nor the names of its contributors may
be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAMIEN BATY BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--- 8< ---
"""
import os
import os.path
import sys
import time
import getopt
import logging
from PIL import Image, ImageFile
## See http://mail.python.org/pipermail/image-sig/1999-August/000816.html
ImageFile.MAXBLOCK = 1000000
## Load drivers so that we can list available image formats
Image.init()
IMAGE_EXTENSIONS = ('jpg', 'gif', 'png')
DEFAULT_OUTPUT_DIRECTORY = 'resized'
DEFAULT_QUALITY = 95
USAGE = """Standard usage: %s [-h,--help]
[-s <width>,<height>]
[-o <output-dir>]
[-a <suffix>]
[-f <image-format>]
[-q <quality>]
<file1> [<file2>...<fileN>]
This program resizes and/or converts all images found amongst the
given files. Generated files are created in a specific directory, but
you may arrange so that the original files are overwritten. Original
and generated images have the same basename, unless you have specified
a different file format.
-h, --help
print this message.
-s <width>,<height>
size of generated images. If <width> or <height> is omitted, the
program proportionnaly computes the missing value from the given
one. However, you must provide at least one of these values.
Default: [image is not resized]
-o <output-dir>
the directory where images are created.
Default: %s
-a <suffix>
suffix to append to the filename (just before the extension). Be
sure to quote the suffix so that it is not parsed as an option.
Example: '-a "-small"' would generate files named '1-small.jpg',
'2.new-small.jpg', etc.
Default: [no suffix]
-f <image-format>
format of the generatd images. The following formats are available: %s
Default: [same as the original image]
-q <quality>
quality of generated JPEG files, on a scale from 1 (worst) to 95
(best). Values above 95 should be avoided; 100 completely disables
the JPEG quantization stage (from PIL documentation).
Default: %s""" % \
(sys.argv[0], DEFAULT_OUTPUT_DIRECTORY,
', '.join(sorted(Image.SAVE.keys())), DEFAULT_QUALITY)
LOGGING_LEVEL = logging.INFO
LOGGING_FORMAT = '%(asctime)s %(levelname)-8s %(message)s'
LOGGING_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
def usage():
print USAGE
def processFile(infile, outfile, suffix,
width, height,
image_format,
quality):
"""Resize and/or convert ``infile``.
If ``suffix`` is given, is is appended to ``outfile``, just before
the extension.
If ``width`` or ``height`` is omitted, the missing value is
computed from the given one so that proportions are kept.
``image_format`` is the format of the generated thumbnail
(``JPEG``, ``PNG``, see PIL documentation). If ``None``, we use
the format of the original image.
"""
try:
image = Image.open(infile)
except IOError, exc:
logging.error('Could not open "%s": Got: %s', infile, exc)
return 0
if image_format is None:
image_format = image.format
## Change ``outfile`` basename if asked
if suffix:
base, ext = outfile.rsplit('.', 1)
outfile = ''.join((base, suffix, '.', ext))
if image_format != image.format:
outfile = ''.join((outfile.rsplit('.', 1)[0],
'.',
image_format.lower()))
## Resize if asked
if width or height:
if not width:
width = image.size[0] * height / float(image.size[1])
width = int(round(width))
elif not height:
height = image.size[1] * width / float(image.size[0])
height = int(round(height))
size = (width, height)
new = image.resize(size, Image.ANTIALIAS)
del image ## This let us overwrite the image if wanted.
image = new
else:
size = image.size
## Finally, save file
try:
image.save(outfile, image_format, quality=quality, optimize=True)
except IOError, exc:
logging.error('Could not save "%s": Got: %s', outfile, exc)
return 0
logging.info('Created "%s" (size: %d,%d).', outfile, size[0], size[1])
return 1
def main():
"""Main function."""
try:
options, args = getopt.getopt(sys.argv[1:],
'ho:s:f:a:q:',
['help'])
except getopt.GetoptError:
usage()
sys.exit(1)
if not args:
usage()
sys.exit(1)
output_dir = DEFAULT_OUTPUT_DIRECTORY
suffix = None
width, height = None, None
image_format = None
quality = DEFAULT_QUALITY
for option, value in options:
if option == '-o':
output_dir = value
elif option == '-a':
suffix = value
elif option == '-s':
width, height = value.split(',')
if width: width = int(width)
if height: height = int(height)
elif option == '-f':
image_format = value
elif option == '-q':
quality = int(value)
elif option in ('-h', '--help'):
usage()
sys.exit(0)
logging.basicConfig(level=LOGGING_LEVEL,
format=LOGGING_FORMAT,
datefmt=LOGGING_DATE_FORMAT)
if os.path.exists(output_dir):
if not os.path.isdir(output_dir):
logging.error('Error: "%s" is not a directory.',
output_dir)
sys.exit(1)
else:
os.mkdir(output_dir)
generated = 0
errors = 0
start_time = time.time()
for filename in args:
outfile = filename.split(os.sep)[-1]
outfile = os.path.join(output_dir, outfile)
if processFile(filename, outfile, suffix,
width, height, image_format, quality):
generated += 1
else:
errors += 1
elapsed = time.time() - start_time
logging.info('Generated %d images in %.2fs.', generated, elapsed)
if errors:
logging.info('WARNING: there were %d errors.', errors)
if __name__ == "__main__":
main()