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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
# Copyright (C) 2009 www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
# Follows PEP8
import os
import re
import shutil
import subprocess
import Image
import imtools
import system
import thumbnail
try:
import _pyexiv2
except:
_pyexiv2 = None
IMAGE_EXTENSIONS = [
'bmp', 'gif', 'jpe', 'jpeg', 'jpg', 'im', 'pcx', 'png', 'pbm', 'pgm',
'ppm', 'tif', 'tiff', 'xbm']
IMAGE_READ_EXTENSIONS = sorted(IMAGE_EXTENSIONS + [
'cur', 'dcx', 'fli', 'flc', 'fpx', 'gbr', 'gd', 'ico', 'imt', 'mic',
'mcidas', 'pcd', 'psd', 'bw', 'rgb', 'cmyk', 'sun', 'tga', 'xpm',
])
IMAGE_WRITE_EXTENSIONS = sorted(IMAGE_EXTENSIONS + ['eps', 'ps', 'pdf'])
# tiff
RE_TIFF_FIELD = re.compile('\s+(.*?):\s+(.*?)\r?\n')
RE_TIFF_FIELD_IMAGE = re.compile(' Image (.*?):\s+(.*?)$')
TIFF_COMPRESSION = {
'CCITT Group 3': 'g3',
'CCITT Group 4': 'g4',
'Deflate': 'zip',
'JPEG': 'jpeg',
'LZW': 'lzw',
'PackBits': 'packbits',
'None': 'none',
}
TIFF_COMPRESSION_TYPES = TIFF_COMPRESSION.values()
TIFF_COMPRESSION_TYPES.sort()
RAW_EXTENSIONS = ['arw', 'cr2', 'crw', 'dcr', 'dng', 'erf', 'kdc', 'nef',
'orf', 'pef', 'raf', 'sr2', 'srf', 'x3f']
def check_libtiff(compression):
if not(open_libtiff or compression.lower() in ['raw', 'none']):
raise Exception('Libtiff tools are needed for "%s" compression'\
% compression)
def open(uri):
format = imtools.get_format_filename(uri)
local = not system.is_www_file(uri)
# svg, pdf, ...
if local:
image = open_image_without_pil(uri, WITHOUT_PIL)
if image:
image.info['Format'] = image.format
image.format = format
return image
# pil
try:
image = open_image_with_pil(uri)
ok = True
except IOError, message:
ok = False
# interlaced png
if ok and not(Image.VERSION < '1.1.7' and
image.format == 'PNG' and 'interlace' in image.info):
return image
# png, tiff (which pil can only handle partly)
if local:
image = open_image_without_pil(uri, ENHANCE_PIL)
if image:
image.info['Format'] = image.format
image.format = format
return image
else:
image = None
if image is None:
raise IOError(message)
return imtools.open_image(uri)
def open_image_exif(uri):
return imtools.transpose_exif(open(uri))
def open_image_exif_thumb(uri):
if _pyexiv2:
try:
pyexiv2_image = _pyexiv2.read_metadata(uri)
thumb_data = pyexiv2_image.getThumbnailData()
if thumb_data:
return imtools.open_image_data(thumb_data)
except Exception, details:
pass
return open_image_exif(uri)
def open_thumb(filename, image=None, open_image=open_image_exif_thumb,
size=thumbnail.SIZE, save_cache=True, force_size=None):
return thumbnail.open(filename=filename, image=image,
open_image=open_image, size=size, save_cache=save_cache,
force_size=force_size)
def open_image_with_pil(uri):
image = imtools.open_image(uri)
#types which PIL can open, but not load
compression = image.info.get('compression', 'none')
if hasattr(compression, 'startswith') and \
compression.startswith('group'):
# tiff image with group4 compression
check_libtiff('g4') # raise exception if openImage not present
image = open_libtiff(uri)
return image
def open_image_without_pil(filename, method_register):
"""Try to open images which PIL can't handle."""
extension = system.file_extension(filename)
if extension in method_register.extensions:
methods = method_register.get_methods(extension)
for open_method in methods:
image = open_method(filename)
if image:
return image
def open_image_with_command(filename, command, app, extension='png',
temp_ext=None):
"""Open with an external command (such as Inkscape, dcraw, imagemagick).
:param filename: filename, from which a temporary filename will be derived
:type filename: string
:param command: conversion command with optional temp file interpolation
:type command: string
:param extension: file type
:type extension: string
:param temp_ext:
if a temp file can not be specified to the command (eg dcraw),
give the file extension of the command output
:type temp_ext: string
"""
if temp_ext:
# eg dcraw
temp = None
temp_file = system.replace_ext(filename, temp_ext)
else:
# imagemagick, ...
if not extension.startswith('.'):
extension = '.' + extension
temp = system.TempFile(extension)
temp_file = temp.path
command.append(temp_file)
try:
stdout, stderr, err = system.call_out_err_temp(
command,
input=[filename],
output_ext=temp_ext,
)
if not err and os.path.exists(temp_file):
# copy() otherwise temp file can't be deleted
image = Image.open(temp_file).copy()
image.info['Convertor'] = app
return image
finally:
if temp:
temp.close(force_remove=False)
elif temp_ext and os.path.exists(temp_file):
os.remove(temp_file)
message = '%s (%s)\n\n%s: %s\n\n%s: %s\n\n%s: %s' % (
_('Could not open image with %s.') % app, '(%s)' % err,
_('Command'), command,
_('Output'), stdout,
_('Error'), stderr,
)
raise IOError(message)
def register():
"""find_exe needs to happen in a function so BIN_PATHS can be set."""
#libtiff
global TIFFINFO, TIFFINFO, get_info_libtiff, open_libtiff, save_libtiff
TIFFINFO = system.find_exe("tiffinfo")
TIFFCP = system.find_exe("tiffcp")
if TIFFINFO and TIFFCP:
def get_info_libtiff(filename, temp=False):
"""Get tiff info of a file with ``tiffinfo``, which needs to be
installed on your system.
:param filename: name of tiff image file
:type filename: string
:returns: info about the file
:rtype: dict
"""
result = {}
def set(key, value):
key = 'libtiff.' + key.lower().replace(' ', '.')
if not (key in result):
result[key] = value
stdout, stderr, err = system.call_out_err_temp(
(TIFFINFO, filename),
input=[filename],
output=[],
universal_newlines=True,
)
if not err:
for match in RE_TIFF_FIELD.finditer(stdout):
value = match.group(2)
again = RE_TIFF_FIELD_IMAGE.search(value)
if again:
set('Image %s' % again.group(1), again.group(2))
set(match.group(1), value[:again.start()])
else:
set(match.group(1), value)
if not result:
raise IOError('Not a TIFF or MDI file, bad magic number.')
result['compression'] = \
TIFF_COMPRESSION[result['libtiff.compression.scheme']]
if temp:
return result, temp_file
return result
def open_libtiff(filename):
"""Opens a tiff file with ``tiffcp``, which needs to be installed
on your system.
:param filename: name of tiff image file
:type filename: string
:returns: PIL image
:rtype: Image.Image
"""
# get info
info, temp_info = get_info_libtiff(filename, temp=True)
if temp_info:
filename = temp_info.path
# extract
temp = system.TempFile()
command = (TIFFCP, '-c', 'none', '-r', '-1', filename, temp.path)
try:
returncode = system.call(command)
if returncode == 0:
# use copy() otherwise temp file can't be deleted (win)
image = Image.open(temp.path).copy()
image.info.update(info)
image.info['Convertor'] = 'libtiff'
return image
finally:
if temp_info:
temp_info.close()
temp.close(force_remove=False)
raise IOError('Could not extract tiff image with tiffcp.')
def save_libtiff(image, filename, compression=None, **options):
"""Saves a tiff compressed file with tiffcp.
:param image: PIL image
:type image: Image.Image
:param filename: name of tiff image file
:type filename: string
:param compression: g3, g4, jpeg, lzw, tiff_lzw
:type compression: string
:returns: log message
:rtype: string
"""
if compression is None:
compression = image.info['compression']
option = []
if compression in ['raw', 'none']:
image.save(filename, 'tiff', **options)
return ''
elif compression in ['g3', 'g4'] and image.mode != '1':
image = image.convert('1')
elif compression == 'jpeg':
option = ['-r', '16']
if image.mode == 'RGBA':
image = image.convert('RGB')
elif compression == 'tiff_lzw':
compression = 'lzw'
temp = system.TempFile()
temp_c = system.TempFile()
try:
image.save(temp.path, 'tiff', **options)
input = [TIFFCP, '-c', compression]
if option:
input.extend(option)
input.extend([temp.path, temp_c.path])
stdout, stderr, err = system.call_out_err(
input,
universal_newlines=True,
)
finally:
temp.close()
temp_c.close(dest=filename)
if err or stdout or stderr:
raise Exception('tiffcp (%s): %s%s\n%s'
% (err, stdout, stderr, input),
)
return ''
else:
open_libtiff = save_libtiff = get_info_libtiff = None
# inkscape
global INKSCAPE, open_inkscape
INKSCAPE = system.find_exe('inkscape')
if INKSCAPE:
def open_inkscape(filename):
"""Open an Inkscape file."""
command = [INKSCAPE, filename, '-e']
return open_image_with_command(filename, command, 'inkscape')
else:
open_inkscape = None
# imagemagick
global IMAGEMAGICK_IDENTIFY, IMAGEMAGICK_CONVERT, \
open_imagemagick, verify_imagemagick
IMAGEMAGICK_IDENTIFY = system.find_exe('identify')
if IMAGEMAGICK_IDENTIFY:
IMAGEMAGICK_CONVERT = system.find_exe('convert')
else:
IMAGEMAGICK_CONVERT = None
if IMAGEMAGICK_CONVERT:
def open_imagemagick(filename):
"""Open an image with Imagemagick."""
command = [IMAGEMAGICK_CONVERT, filename, '-interlace', 'none',
'-background', 'none', '-flatten']
return open_image_with_command(filename, command, 'imagemagick')
else:
open_imagemagick = None
if IMAGEMAGICK_IDENTIFY:
def verify_imagemagick(filename):
"""Verify an image with Imagemagick."""
command = (IMAGEMAGICK_IDENTIFY, '-quiet', filename)
retcode = system.call(command)
if retcode == 0:
return True
return False
else:
verify_imagemagick = None
# xcf tools (gimp)
global XCF2PNG, XCFINFO, open_xcf, verify_xcf
XCF2PNG = system.find_exe('xcf2png')
XCFINFO = system.find_exe('xcfinfo')
if XCF2PNG:
def open_xcf(filename):
"""Open a gimp file."""
command = [XCF2PNG, filename, '-o']
return open_image_with_command(filename, command, 'xcf2png')
else:
open_xcf = None
if XCFINFO:
def verify_xcf(filename):
"""Verify a gimp file."""
command = (XCFINFO, '-u', filename)
retcode = system.call(command)
if retcode == 0:
return True
return False
else:
verify_xcf = None
# dcraw
global DCRAW, open_dcraw, verify_dcraw
DCRAW = system.find_exe('dcraw')
if DCRAW:
def open_dcraw(filename):
"""Open a camera raw image file."""
command = [DCRAW, '-w', filename]
return open_image_with_command(filename, command, 'dcraw',
temp_ext='ppm')
def verify_dcraw(filename):
"""Verify a camera raw image file."""
command = (DCRAW, '-i', filename)
retcode = system.call(command)
if retcode == 0:
return True
return False
else:
open_dcraw = None
verify_dcraw = None
# register methods
# IMPORTANT: the order of registering is important, the method
# which is first registered gets more priority
global WITHOUT_PIL, ENHANCE_PIL, VERIFY_WITHOUT_PIL
WITHOUT_PIL = system.MethodRegister()
WITHOUT_PIL.register(['xcf'], open_xcf)
WITHOUT_PIL.register(RAW_EXTENSIONS, open_dcraw)
WITHOUT_PIL.register(['svg', 'svgz'], open_inkscape)
WITHOUT_PIL.register(
[
'ai', 'avi', 'cmyk', 'cmyka', 'dpx', 'eps', 'exr', 'mng',
'mov', 'mpeg', 'mpg', 'otf', 'pdf', 'pict', 'ps', 'psd',
'svg', 'svgz', 'ttf', 'wmf', 'xcf', 'xpm', 'ycbcr',
'ycbcra', 'yuv'
],
open_imagemagick,
)
# This is for file formats which PIL can read, but not all subformats
# For example: compressed tiff files
ENHANCE_PIL = system.MethodRegister()
ENHANCE_PIL.register(['tiff'], open_libtiff)
ENHANCE_PIL.register(['png'], open_imagemagick)
VERIFY_WITHOUT_PIL = system.MethodRegister()
VERIFY_WITHOUT_PIL.register(['xcf'], verify_xcf)
VERIFY_WITHOUT_PIL.register(RAW_EXTENSIONS, verify_dcraw)
VERIFY_WITHOUT_PIL.register(
['eps', 'psd', 'pdf', 'svg', 'svgz', 'wmf', 'xcf'],
verify_imagemagick,
)
global IMAGE_EXTENSIONS, IMAGE_READ_EXTENSIONS
# update read extensions
IMAGE_READ_EXTENSIONS = set(IMAGE_READ_EXTENSIONS)\
.union(WITHOUT_PIL.extensions)
IMAGE_READ_EXTENSIONS = sorted(IMAGE_READ_EXTENSIONS)
# update read and write extensions
IMAGE_EXTENSIONS = [ext for ext in IMAGE_READ_EXTENSIONS
if ext in IMAGE_WRITE_EXTENSIONS]
global verify_image
def verify_image(info_file, valid, invalid,
method_register=VERIFY_WITHOUT_PIL):
extension = system.file_extension(info_file['path'])
if extension in method_register.extensions:
verify_image_without_pil(info_file, method_register, valid,
invalid)
else:
verify_image_with_pil(info_file, valid, invalid)
def verify_image_with_pil(info_file, valid, invalid):
try:
im = open(info_file['path'])
#if info has 'Convertor', the image is not opened by PIL
#and already loaded and verified
if not ('Convertor' in im.info):
im.verify()
valid.append(info_file)
return True
except Exception, error:
invalid.append(info_file['path'])
return False
def verify_image_without_pil(info_file, method_register, valid, invalid):
"""Try to verify images which PIL can't handle."""
extension = system.file_extension(info_file['path'])
methods = method_register.get_methods(extension)
for verify_method in methods:
if verify_method(info_file['path']):
valid.append(info_file)
return True
invalid.append(info_file['path'])
return False
if __name__ == '__main__':
print TIFF_COMPRESSION_TYPES
filename = '/home/stani/Downloads/0009.tif'
image = open(filename)
print(image.info)
#save(image.rotate(10), filename + '.tif', compression='g4')
|