14
14
# directory is placed first in the file.
18
# 96-05-04 fl Handle JPEGTABLES tag
19
# 96-05-18 fl Fixed COLORMAP support
20
# 97-01-05 fl Fixed PREDICTOR support
21
# 97-08-27 fl Added support for rational tags (from Perry Stoll)
22
# 98-01-10 fl Fixed seek/tell (from Jan Blom)
23
# 98-07-15 fl Use private names for internal variables
24
# 99-06-13 fl Rewritten for PIL 1.0
25
# 00-10-11 fl Additional fixes for Python 2.0
17
# 1995-09-01 fl Created
18
# 1996-05-04 fl Handle JPEGTABLES tag
19
# 1996-05-18 fl Fixed COLORMAP support
20
# 1997-01-05 fl Fixed PREDICTOR support
21
# 1997-08-27 fl Added support for rational tags (from Perry Stoll)
22
# 1998-01-10 fl Fixed seek/tell (from Jan Blom)
23
# 1998-07-15 fl Use private names for internal variables
24
# 1999-06-13 fl Rewritten for PIL 1.0 (1.0)
25
# 2000-10-11 fl Additional fixes for Python 2.0 (1.1)
26
# 2001-04-17 fl Fixed rewind support (seek to frame 0) (1.2)
27
# 2001-05-12 fl Added write support for more tags (from Greg Couch) (1.3)
28
# 2001-12-18 fl Added workaround for broken Matrox library
29
# 2002-01-18 fl Don't mess up if photometric tag is missing (D. Alan Stewart)
27
# Copyright (c) Secret Labs AB 1997-99.
28
# Copyright (c) Fredrik Lundh 1995-97.
31
# Copyright (c) 1997-2002 by Secret Labs AB
32
# Copyright (c) 1995-1997 by Fredrik Lundh
30
34
# See the README file for information on usage and redistribution.
35
39
import Image, ImageFile
36
40
import ImagePalette
163
174
value = self[tag]
164
175
if len(value) != 1:
176
if tag == SAMPLEFORMAT:
177
# work around broken (?) matrox library
178
# (from Ted Wright, via Bob Klimek)
179
raise KeyError # use default
165
180
raise ValueError, "not a scalar"
313
328
if tag == STRIPOFFSETS:
314
329
stripoffsets = len(directory)
315
330
typ = 4 # to avoid catch-22
331
elif tag in (X_RESOLUTION, Y_RESOLUTION):
332
# identify rational data fields
329
347
elif len(data) < 4:
330
348
append((tag, typ, len(value), data + (4-len(data))*"\0", ""))
332
append((tag, typ, len(value), o32(offset), data))
352
count = count / 2 # adjust for rational data field
353
append((tag, typ, count, o32(offset), data))
333
354
offset = offset + len(data)
335
356
offset = offset + 1 # word padding
396
419
self.fp = self.__fp
397
420
if frame < self.__frame:
400
423
self.__next = self.__first
401
424
while self.__frame < frame:
426
raise EOFError, "no more images in TIFF file"
402
427
self.fp.seek(self.__next)
403
428
self.tag.load(self.fp)
404
429
self.__next = self.tag.next
551
578
"LAB": ("LAB", 8, 1, (8,8,8), None),
582
# convert value to TIFF rational number -- (numerator, denominator)
583
if type(value) in (type([]), type(())):
584
assert(len(value) % 2 == 0)
586
if type(value) == type(1):
589
return (int(value * 65536), 65536)
554
591
def _save(im, fp, filename):
566
603
ifd[IMAGEWIDTH] = im.size[0]
567
604
ifd[IMAGELENGTH] = im.size[1]
606
# additions written by Greg Couch, gregc@cgl.ucsf.edu
607
# inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
608
if hasattr(im, 'tag'):
609
# preserve tags from original TIFF image file
610
for key in (RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION):
611
if im.tag.tagdata.has_key(key):
612
ifd[key] = im.tag.tagdata.get(key)
569
613
if im.encoderinfo.has_key("description"):
570
614
ifd[IMAGEDESCRIPTION] = im.encoderinfo["description"]
615
if im.encoderinfo.has_key("resolution"):
616
ifd[X_RESOLUTION] = ifd[Y_RESOLUTION] \
617
= _cvt_res(im.encoderinfo["resolution"])
618
if im.encoderinfo.has_key("x resolution"):
619
ifd[X_RESOLUTION] = _cvt_res(im.encoderinfo["x resolution"])
620
if im.encoderinfo.has_key("y resolution"):
621
ifd[Y_RESOLUTION] = _cvt_res(im.encoderinfo["y resolution"])
622
if im.encoderinfo.has_key("resolution unit"):
623
unit = im.encoderinfo["resolution unit"]
625
ifd[RESOLUTION_UNIT] = 2
626
elif unit == "cm" or unit == "centimeter":
627
ifd[RESOLUTION_UNIT] = 3
629
ifd[RESOLUTION_UNIT] = 1
630
if im.encoderinfo.has_key("software"):
631
ifd[SOFTWARE] = im.encoderinfo["software"]
632
if im.encoderinfo.has_key("date time"):
633
ifd[DATE_TIME] = im.encoderinfo["date time"]
634
if im.encoderinfo.has_key("artist"):
635
ifd[ARTIST] = im.encoderinfo["artist"]
636
if im.encoderinfo.has_key("copyright"):
637
ifd[COPYRIGHT] = im.encoderinfo["copyright"]
573
640
ifd[BITSPERSAMPLE] = bits