~edwin-grubbs/python-imaging/trunk

« back to all changes in this revision

Viewing changes to PIL/TiffImagePlugin.py

  • Committer: effbot
  • Date: 2006-03-01 19:11:48 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:18
Load Imaging-1.1.3 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
# The Python Imaging Library.
3
 
# $Id$
 
3
# $Id: //modules/pil/PIL/TiffImagePlugin.py#5 $
4
4
#
5
5
# TIFF file handling
6
6
#
14
14
# directory is placed first in the file.
15
15
#
16
16
# History:
17
 
# 95-09-01 fl   Created
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)
26
30
#
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
29
33
#
30
34
# See the README file for information on usage and redistribution.
31
35
#
32
36
 
33
 
__version__ = "1.1"
 
37
__version__ = "1.3.2"
34
38
 
35
39
import Image, ImageFile
36
40
import ImagePalette
66
70
SAMPLESPERPIXEL = 277
67
71
ROWSPERSTRIP = 278
68
72
STRIPBYTECOUNTS = 279
 
73
X_RESOLUTION = 282
 
74
Y_RESOLUTION = 283
69
75
PLANAR_CONFIGURATION = 284
 
76
RESOLUTION_UNIT = 296
 
77
SOFTWARE = 305
 
78
DATE_TIME = 306
 
79
ARTIST = 315
70
80
PREDICTOR = 317
71
81
COLORMAP = 320
72
82
EXTRASAMPLES = 338
73
83
SAMPLEFORMAT = 339
74
84
JPEGTABLES = 347
 
85
COPYRIGHT = 33432
75
86
IPTC_NAA_CHUNK = 33723 # newsphoto properties
76
87
PHOTOSHOP_CHUNK = 34377 # photoshop properties
77
88
 
162
173
        try:
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"
166
181
            return value[0]
167
182
        except KeyError:
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
 
333
                    typ = 5
316
334
                else:
317
335
                    typ = 3
318
336
                    for v in value:
329
347
            elif len(data) < 4:
330
348
                append((tag, typ, len(value), data + (4-len(data))*"\0", ""))
331
349
            else:
332
 
                append((tag, typ, len(value), o32(offset), data))
 
350
                count = len(value)
 
351
                if typ == 5:
 
352
                    count = count / 2        # adjust for rational data field
 
353
                append((tag, typ, count, o32(offset), data))
333
354
                offset = offset + len(data)
334
355
                if offset & 1:
335
356
                    offset = offset + 1 # word padding
384
405
    def seek(self, frame):
385
406
        "Select a given frame as current image"
386
407
 
 
408
        if frame < 0:
 
409
            frame = 0
387
410
        self._seek(frame)
388
411
 
389
412
    def tell(self):
396
419
        self.fp = self.__fp
397
420
        if frame < self.__frame:
398
421
            # rewind file
399
 
            self.__frame = 0
 
422
            self.__frame = -1
400
423
            self.__next = self.__first
401
424
        while self.__frame < frame:
 
425
            if not self.__next:
 
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
439
464
            PLANAR_CONFIGURATION, 1
440
465
            )
441
466
 
442
 
        photo = self.tag.getscalar(PHOTOMETRIC_INTERPRETATION)
 
467
        # photometric is a required tag, but not everyone is reading
 
468
        # the specification
 
469
        photo = self.tag.getscalar(PHOTOMETRIC_INTERPRETATION, 0)
443
470
 
444
471
        if Image.DEBUG:
445
472
            print "*** Summary ***"
551
578
    "LAB": ("LAB", 8, 1, (8,8,8), None),
552
579
}
553
580
 
 
581
def _cvt_res(value):
 
582
    # convert value to TIFF rational number -- (numerator, denominator)
 
583
    if type(value) in (type([]), type(())):
 
584
        assert(len(value) % 2 == 0)
 
585
        return value
 
586
    if type(value) == type(1):
 
587
        return (value, 1)
 
588
    value = float(value)
 
589
    return (int(value * 65536), 65536)
 
590
 
554
591
def _save(im, fp, filename):
555
592
 
556
593
    try:
566
603
    ifd[IMAGEWIDTH] = im.size[0]
567
604
    ifd[IMAGELENGTH] = im.size[1]
568
605
 
 
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"]
 
624
        if unit == "inch":
 
625
            ifd[RESOLUTION_UNIT] = 2
 
626
        elif unit == "cm" or unit == "centimeter":
 
627
            ifd[RESOLUTION_UNIT] = 3
 
628
        else:
 
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"]
571
638
 
572
639
    if bits != (1,):
573
640
        ifd[BITSPERSAMPLE] = bits