2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# TIFF file handling
|
|
|
|
#
|
|
|
|
# TIFF is a flexible, if somewhat aged, image file format originally
|
|
|
|
# defined by Aldus. Although TIFF supports a wide variety of pixel
|
|
|
|
# layouts and compression methods, the name doesn't really stand for
|
|
|
|
# "thousands of incompatible file formats," it just feels that way.
|
|
|
|
#
|
|
|
|
# To read TIFF data from a stream, the stream must be seekable. For
|
|
|
|
# progressive decoding, make sure to use TIFF files where the tag
|
|
|
|
# directory is placed first in the file.
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1995-09-01 fl Created
|
|
|
|
# 1996-05-04 fl Handle JPEGTABLES tag
|
|
|
|
# 1996-05-18 fl Fixed COLORMAP support
|
|
|
|
# 1997-01-05 fl Fixed PREDICTOR support
|
|
|
|
# 1997-08-27 fl Added support for rational tags (from Perry Stoll)
|
|
|
|
# 1998-01-10 fl Fixed seek/tell (from Jan Blom)
|
|
|
|
# 1998-07-15 fl Use private names for internal variables
|
|
|
|
# 1999-06-13 fl Rewritten for PIL 1.0 (1.0)
|
|
|
|
# 2000-10-11 fl Additional fixes for Python 2.0 (1.1)
|
|
|
|
# 2001-04-17 fl Fixed rewind support (seek to frame 0) (1.2)
|
|
|
|
# 2001-05-12 fl Added write support for more tags (from Greg Couch) (1.3)
|
|
|
|
# 2001-12-18 fl Added workaround for broken Matrox library
|
|
|
|
# 2002-01-18 fl Don't mess up if photometric tag is missing (D. Alan Stewart)
|
|
|
|
# 2003-05-19 fl Check FILLORDER tag
|
|
|
|
# 2003-09-26 fl Added RGBa support
|
|
|
|
# 2004-02-24 fl Added DPI support; fixed rational write support
|
|
|
|
# 2005-02-07 fl Added workaround for broken Corel Draw 10 files
|
|
|
|
# 2006-01-09 fl Added support for float/double tags (from Russell Nelson)
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2006 by Secret Labs AB. All rights reserved.
|
|
|
|
# Copyright (c) 1995-1997 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
from __future__ import division, print_function
|
2012-10-16 06:27:35 +04:00
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import Image, ImageFile
|
|
|
|
from PIL import ImagePalette
|
|
|
|
from PIL import _binary
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2012-10-16 01:19:55 +04:00
|
|
|
import collections
|
2014-12-29 18:48:01 +03:00
|
|
|
from fractions import Fraction
|
|
|
|
import io
|
2012-10-16 01:19:55 +04:00
|
|
|
import itertools
|
2014-12-29 18:48:01 +03:00
|
|
|
from numbers import Number
|
2013-03-09 07:51:59 +04:00
|
|
|
import os
|
2014-12-29 18:48:01 +03:00
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import warnings
|
|
|
|
|
2015-09-13 17:15:13 +03:00
|
|
|
from .TiffTags import TAGS_V2, TYPES, TagInfo
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-08-25 15:27:18 +03:00
|
|
|
__version__ = "1.3.5"
|
|
|
|
DEBUG = False # Needs to be merged with the new logging approach.
|
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
# Set these to true to force use of libtiff for reading or writing.
|
2013-11-22 08:33:16 +04:00
|
|
|
READ_LIBTIFF = False
|
2014-07-28 20:00:06 +04:00
|
|
|
WRITE_LIBTIFF = False
|
2015-09-10 18:49:37 +03:00
|
|
|
IFD_LEGACY_API = True
|
2013-11-22 08:33:16 +04:00
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
II = b"II" # little-endian (Intel style)
|
|
|
|
MM = b"MM" # big-endian (Motorola style)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
i8 = _binary.i8
|
|
|
|
o8 = _binary.o8
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Read TIFF files
|
|
|
|
|
|
|
|
# a few tag names, just to make the code below a bit more readable
|
|
|
|
IMAGEWIDTH = 256
|
|
|
|
IMAGELENGTH = 257
|
|
|
|
BITSPERSAMPLE = 258
|
|
|
|
COMPRESSION = 259
|
|
|
|
PHOTOMETRIC_INTERPRETATION = 262
|
|
|
|
FILLORDER = 266
|
|
|
|
IMAGEDESCRIPTION = 270
|
|
|
|
STRIPOFFSETS = 273
|
|
|
|
SAMPLESPERPIXEL = 277
|
|
|
|
ROWSPERSTRIP = 278
|
|
|
|
STRIPBYTECOUNTS = 279
|
|
|
|
X_RESOLUTION = 282
|
|
|
|
Y_RESOLUTION = 283
|
|
|
|
PLANAR_CONFIGURATION = 284
|
|
|
|
RESOLUTION_UNIT = 296
|
|
|
|
SOFTWARE = 305
|
|
|
|
DATE_TIME = 306
|
|
|
|
ARTIST = 315
|
|
|
|
PREDICTOR = 317
|
|
|
|
COLORMAP = 320
|
|
|
|
TILEOFFSETS = 324
|
|
|
|
EXTRASAMPLES = 338
|
|
|
|
SAMPLEFORMAT = 339
|
|
|
|
JPEGTABLES = 347
|
|
|
|
COPYRIGHT = 33432
|
2014-07-28 20:00:06 +04:00
|
|
|
IPTC_NAA_CHUNK = 33723 # newsphoto properties
|
|
|
|
PHOTOSHOP_CHUNK = 34377 # photoshop properties
|
2010-07-31 06:52:47 +04:00
|
|
|
ICCPROFILE = 34675
|
|
|
|
EXIFIFD = 34665
|
|
|
|
XMP = 700
|
|
|
|
|
2013-09-27 21:41:27 +04:00
|
|
|
# https://github.com/fiji/ImageJA/blob/master/src/main/java/ij/io/TiffDecoder.java
|
|
|
|
IMAGEJ_META_DATA_BYTE_COUNTS = 50838
|
|
|
|
IMAGEJ_META_DATA = 50839
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
COMPRESSION_INFO = {
|
|
|
|
# Compression => pil compression name
|
|
|
|
1: "raw",
|
|
|
|
2: "tiff_ccitt",
|
|
|
|
3: "group3",
|
|
|
|
4: "group4",
|
|
|
|
5: "tiff_lzw",
|
2014-07-28 20:00:06 +04:00
|
|
|
6: "tiff_jpeg", # obsolete
|
2010-07-31 06:52:47 +04:00
|
|
|
7: "jpeg",
|
2013-07-01 18:45:42 +04:00
|
|
|
8: "tiff_adobe_deflate",
|
2014-07-28 20:00:06 +04:00
|
|
|
32771: "tiff_raw_16", # 16-bit padding
|
2013-07-01 18:45:42 +04:00
|
|
|
32773: "packbits",
|
|
|
|
32809: "tiff_thunderscan",
|
|
|
|
32946: "tiff_deflate",
|
|
|
|
34676: "tiff_sgilog",
|
|
|
|
34677: "tiff_sgilog24",
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
COMPRESSION_INFO_REV = dict([(v, k) for (k, v) in COMPRESSION_INFO.items()])
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
OPEN_INFO = {
|
|
|
|
# (ByteOrder, PhotoInterpretation, SampleFormat, FillOrder, BitsPerSample,
|
|
|
|
# ExtraSamples) => mode, rawmode
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 0, (1,), 1, (1,), ()): ("1", "1;I"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 0, (1,), 1, (1,), ()): ("1", "1;I"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 0, (1,), 2, (1,), ()): ("1", "1;IR"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 0, (1,), 2, (1,), ()): ("1", "1;IR"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 0, (1,), 1, (8,), ()): ("L", "L;I"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 0, (1,), 1, (8,), ()): ("L", "L;I"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 0, (1,), 2, (8,), ()): ("L", "L;IR"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 0, (1,), 2, (8,), ()): ("L", "L;IR"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 0, (3,), 1, (32,), ()): ("F", "F;32F"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 0, (3,), 1, (32,), ()): ("F", "F;32BF"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (1,), ()): ("1", "1"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 1, (1,), ()): ("1", "1"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (4,), ()): ("L", "L;4"),
|
2015-06-17 00:27:02 +03:00
|
|
|
# ?
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 2, (1,), ()): ("1", "1;R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 2, (1,), ()): ("1", "1;R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (8,), ()): ("L", "L"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 1, (8,), ()): ("L", "L"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (8, 8), (2,)): ("LA", "LA"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 1, (8, 8), (2,)): ("LA", "LA"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 2, (8,), ()): ("L", "L;R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 2, (8,), ()): ("L", "L;R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (12,), ()): ("I;16", "I;12"),
|
|
|
|
(II, 1, (1,), 1, (16,), ()): ("I;16", "I;16"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (1,), 1, (16,), ()): ("I;16B", "I;16B"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (2,), 1, (16,), ()): ("I;16S", "I;16S"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(MM, 1, (2,), 1, (16,), ()): ("I;16BS", "I;16BS"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(II, 1, (1,), 1, (32,), ()): ("I", "I;32N"),
|
|
|
|
(II, 1, (2,), 1, (32,), ()): ("I", "I;32S"),
|
|
|
|
(MM, 1, (2,), 1, (32,), ()): ("I;32BS", "I;32BS"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 1, (3,), 1, (32,), ()): ("F", "F;32F"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 1, (3,), 1, (32,), ()): ("F", "F;32BF"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 2, (8, 8, 8), ()): ("RGB", "RGB;R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 2, (8, 8, 8), ()): ("RGB", "RGB;R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8, 8), ()): ("RGBA", "RGBA"), # missing ExtraSamples
|
2015-06-17 00:16:56 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8, 8), ()): ("RGBA", "RGBA"), # missing ExtraSamples
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8, 8), (0,)): ("RGBX", "RGBX"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8, 8), (0,)): ("RGBX", "RGBX"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1,), 1, (8, 8, 8, 8), (999,)): ("RGBA", "RGBA"), # Corel Draw 10
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 2, (1,), 1, (8, 8, 8, 8), (999,)): ("RGBA", "RGBA"), # Corel Draw 10
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 2, (1, 1, 1, 1), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBA"), # OSX Grab
|
2015-06-17 00:16:56 +03:00
|
|
|
(MM, 2, (1, 1, 1, 1), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBA"), # OSX Grab
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 1, (1,), ()): ("P", "P;1"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 1, (1,), ()): ("P", "P;1"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 2, (1,), ()): ("P", "P;1R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 2, (1,), ()): ("P", "P;1R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 1, (2,), ()): ("P", "P;2"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 1, (2,), ()): ("P", "P;2"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 2, (2,), ()): ("P", "P;2R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 2, (2,), ()): ("P", "P;2R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 1, (4,), ()): ("P", "P;4"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 1, (4,), ()): ("P", "P;4"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 2, (4,), ()): ("P", "P;4R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 2, (4,), ()): ("P", "P;4R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 1, (8,), ()): ("P", "P"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 1, (8,), ()): ("P", "P"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 3, (1,), 2, (8,), ()): ("P", "P;R"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 3, (1,), 2, (8,), ()): ("P", "P;R"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 5, (1,), 1, (8, 8, 8, 8), ()): ("CMYK", "CMYK"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 5, (1,), 1, (8, 8, 8, 8), ()): ("CMYK", "CMYK"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 6, (1,), 1, (8, 8, 8), ()): ("YCbCr", "YCbCr"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 6, (1,), 1, (8, 8, 8), ()): ("YCbCr", "YCbCr"),
|
2015-06-17 00:27:02 +03:00
|
|
|
(II, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(MM, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
PREFIXES = [b"MM\000\052", b"II\052\000", b"II\xBC\000"]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
|
|
|
return prefix[:4] in PREFIXES
|
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def _limit_rational(val, max_val):
|
|
|
|
inv = abs(val) > 1
|
|
|
|
f = Fraction.from_float(1 / val if inv else val).limit_denominator(max_val)
|
|
|
|
n_d = (f.numerator, f.denominator)
|
|
|
|
return n_d[::-1] if inv else n_d
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Wrapper for TIFF IFDs.
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
_load_dispatch = {}
|
|
|
|
_write_dispatch = {}
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
class ImageFileDirectory_v2(collections.MutableMapping):
|
2014-12-29 18:48:01 +03:00
|
|
|
"""This class represents a TIFF tag directory. To speed things up, we
|
|
|
|
don't decode tags unless they're asked for.
|
|
|
|
|
|
|
|
Exposes a dictionary interface of the tags in the directory
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-08 03:59:37 +04:00
|
|
|
ImageFileDirectory[key] = value
|
|
|
|
value = ImageFileDirectory[key]
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
Also contains a dictionary of tag types as read from the tiff image file,
|
|
|
|
'ImageFileDirectory.tagtype'
|
2013-10-08 03:59:37 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
Data Structures:
|
2013-10-08 03:59:37 +04:00
|
|
|
'public'
|
|
|
|
* self.tagtype = {} Key: numerical tiff tag number
|
|
|
|
Value: integer corresponding to the data type from
|
|
|
|
`TiffTags.TYPES`
|
2015-09-13 16:01:01 +03:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Documentation:
|
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
'internal'
|
2015-09-13 16:01:01 +03:00
|
|
|
* self._tags_v2 = {} Key: numerical tiff tag number
|
|
|
|
Value: decoded data, as tuple for multiple values
|
2014-12-29 18:48:01 +03:00
|
|
|
* self._tagdata = {} Key: numerical tiff tag number
|
|
|
|
Value: undecoded byte string from file
|
2015-09-13 16:01:01 +03:00
|
|
|
* self._tags_v1 = {} Key: numerical tiff tag number
|
|
|
|
Value: decoded data in the v1 format
|
|
|
|
|
2015-03-01 06:44:38 +03:00
|
|
|
Tags will be found in the private attributes self._tagdata, and in
|
2015-09-13 16:01:01 +03:00
|
|
|
self._tags_v2 once decoded.
|
|
|
|
|
|
|
|
If legacy_api is true, then decoded tags will be populated into both
|
|
|
|
_tags_v1 and _tags_v2. _Tags_v2 will be used if this IFD is used in
|
|
|
|
the TIFF save routine.
|
|
|
|
|
|
|
|
Tags will be read from tags_v1 if legacy_api == true.
|
2014-12-29 18:48:01 +03:00
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-30 13:57:45 +03:00
|
|
|
def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None):
|
|
|
|
"""Initialize an ImageFileDirectory.
|
|
|
|
|
|
|
|
To construct an ImageFileDirectory from a real file, pass the 8-byte
|
|
|
|
magic header to the constructor. To only set the endianness, pass it
|
|
|
|
as the 'prefix' keyword argument.
|
|
|
|
|
|
|
|
:ifh: One of the accepted magic headers (cf. PREFIXES); also sets
|
|
|
|
endianness.
|
|
|
|
:prefix: Override the endianness of the file.
|
2013-10-08 03:59:37 +04:00
|
|
|
"""
|
2014-12-30 13:57:45 +03:00
|
|
|
if ifh[:4] not in PREFIXES:
|
|
|
|
raise SyntaxError("not a TIFF file (header %r not valid)" % ifh)
|
|
|
|
self._prefix = prefix if prefix is not None else ifh[:2]
|
|
|
|
if self._prefix == MM:
|
2014-12-29 18:48:01 +03:00
|
|
|
self._endian = ">"
|
2014-12-30 13:57:45 +03:00
|
|
|
elif self._prefix == II:
|
2014-12-29 18:48:01 +03:00
|
|
|
self._endian = "<"
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2014-12-30 13:57:45 +03:00
|
|
|
raise SyntaxError("not a TIFF IFD")
|
2010-07-31 06:52:47 +04:00
|
|
|
self.reset()
|
2014-12-30 13:57:45 +03:00
|
|
|
self.next, = self._unpack("L", ifh[4:])
|
2015-09-11 20:09:14 +03:00
|
|
|
self._legacy_api = False
|
2015-09-13 16:01:01 +03:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
prefix = property(lambda self: self._prefix)
|
|
|
|
offset = property(lambda self: self._offset)
|
2015-03-01 06:44:38 +03:00
|
|
|
legacy_api = property(lambda self: self._legacy_api)
|
|
|
|
|
|
|
|
@legacy_api.setter
|
|
|
|
def legacy_api(self, value):
|
2015-09-13 16:01:01 +03:00
|
|
|
raise Exception("Not allowing setting of legacy api")
|
2014-12-29 18:48:01 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def reset(self):
|
2015-09-13 16:01:01 +03:00
|
|
|
self._tags_v1 = {} # will remain empty if legacy_api is false
|
|
|
|
self._tags_v2 = {} # main tag storage
|
2014-12-29 18:48:01 +03:00
|
|
|
self._tagdata = {}
|
2014-07-28 20:00:06 +04:00
|
|
|
self.tagtype = {} # added 2008-06-05 by Florian Hoech
|
2014-12-29 18:48:01 +03:00
|
|
|
self._next = None
|
|
|
|
self._offset = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-07 15:59:52 +04:00
|
|
|
def __str__(self):
|
2014-12-29 18:48:01 +03:00
|
|
|
return str(dict(self))
|
2013-03-07 15:59:52 +04:00
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
"""Return a dictionary of the image's tags."""
|
2014-12-29 18:48:01 +03:00
|
|
|
# FIXME Deprecate: use dict(self)
|
|
|
|
return dict(self)
|
2013-03-07 15:59:52 +04:00
|
|
|
|
|
|
|
def named(self):
|
2014-07-28 20:00:06 +04:00
|
|
|
"""
|
2015-05-29 07:59:54 +03:00
|
|
|
Returns the complete tag dictionary, with named tags where possible.
|
2014-07-28 20:00:06 +04:00
|
|
|
"""
|
2015-09-13 17:15:13 +03:00
|
|
|
return dict((TAGS_V2.get(code, TagInfo()).name, value)
|
2015-01-14 10:07:41 +03:00
|
|
|
for code, value in self.items())
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __len__(self):
|
2015-09-13 16:01:01 +03:00
|
|
|
return len(set(self._tagdata) | set(self._tags_v2))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __getitem__(self, tag):
|
2015-09-13 16:01:01 +03:00
|
|
|
if tag not in self._tags_v2: # unpack on the fly
|
2014-12-29 18:48:01 +03:00
|
|
|
data = self._tagdata[tag]
|
|
|
|
typ = self.tagtype[tag]
|
|
|
|
size, handler = self._load_dispatch[typ]
|
2015-09-13 13:07:43 +03:00
|
|
|
self[tag] = handler(self, self.legacy_api, data) # check type
|
2015-09-13 16:01:01 +03:00
|
|
|
val = self._tags_v2[tag]
|
2015-03-01 06:44:38 +03:00
|
|
|
if self.legacy_api and not isinstance(val, (tuple, bytes)):
|
|
|
|
val = val,
|
|
|
|
return val
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2012-10-16 01:19:55 +04:00
|
|
|
def __contains__(self, tag):
|
2015-09-13 16:01:01 +03:00
|
|
|
return tag in self._tags_v2 or tag in self._tagdata
|
2012-10-16 01:19:55 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if bytes is str:
|
2012-10-16 01:19:55 +04:00
|
|
|
def has_key(self, tag):
|
|
|
|
return tag in self
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __setitem__(self, tag, value):
|
2015-09-13 16:01:01 +03:00
|
|
|
self._setitem(tag, value, self.legacy_api)
|
|
|
|
|
|
|
|
def _setitem(self, tag, value, legacy_api):
|
2014-12-29 18:48:01 +03:00
|
|
|
basetypes = (Number, bytes, str)
|
|
|
|
if bytes is str:
|
|
|
|
basetypes += unicode,
|
2012-10-16 01:19:55 +04:00
|
|
|
|
2015-09-13 17:15:13 +03:00
|
|
|
info = TAGS_V2.get(tag, TagInfo())
|
2014-12-29 18:48:01 +03:00
|
|
|
values = [value] if isinstance(value, basetypes) else value
|
2013-02-26 15:12:11 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
if tag not in self.tagtype:
|
|
|
|
try:
|
|
|
|
self.tagtype[tag] = info.type
|
|
|
|
except KeyError:
|
|
|
|
self.tagtype[tag] = 7
|
|
|
|
if all(isinstance(v, int) for v in values):
|
|
|
|
if all(v < 2 ** 16 for v in values):
|
|
|
|
self.tagtype[tag] = 3
|
|
|
|
else:
|
|
|
|
self.tagtype[tag] = 4
|
|
|
|
elif all(isinstance(v, float) for v in values):
|
|
|
|
self.tagtype[tag] = 12
|
|
|
|
else:
|
|
|
|
if bytes is str:
|
|
|
|
# Never treat data as binary by default on Python 2.
|
|
|
|
self.tagtype[tag] = 2
|
|
|
|
else:
|
|
|
|
if all(isinstance(v, str) for v in values):
|
|
|
|
self.tagtype[tag] = 2
|
|
|
|
|
|
|
|
if self.tagtype[tag] == 7 and bytes is not str:
|
2015-09-12 01:44:23 +03:00
|
|
|
values = [value.encode("ascii",'replace') if isinstance(value, str) else value
|
2014-12-29 18:48:01 +03:00
|
|
|
for value in values]
|
2015-09-13 16:01:01 +03:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
values = tuple(info.cvt_enum(value) for value in values)
|
2015-09-13 16:01:01 +03:00
|
|
|
|
|
|
|
dest = self._tags_v1 if legacy_api else self._tags_v2
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
if info.length == 1:
|
2015-09-13 16:01:01 +03:00
|
|
|
if legacy_api and self.tagtype[tag] in [5, 10]:
|
2015-03-01 06:44:38 +03:00
|
|
|
values = values,
|
2015-09-13 16:01:01 +03:00
|
|
|
dest[tag], = values
|
2014-12-29 18:48:01 +03:00
|
|
|
else:
|
2015-09-13 16:01:01 +03:00
|
|
|
dest[tag] = values
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def __delitem__(self, tag):
|
2015-09-13 16:01:01 +03:00
|
|
|
self._tags_v2.pop(tag, None)
|
|
|
|
self._tags_v1.pop(tag, None)
|
2014-12-29 18:48:01 +03:00
|
|
|
self._tagdata.pop(tag, None)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def __iter__(self):
|
2015-09-13 16:01:01 +03:00
|
|
|
return iter(set(self._tagdata) | set(self._tags_v2))
|
2014-12-29 18:48:01 +03:00
|
|
|
|
2014-12-30 13:57:45 +03:00
|
|
|
def _unpack(self, fmt, data):
|
2014-12-29 18:48:01 +03:00
|
|
|
return struct.unpack(self._endian + fmt, data)
|
|
|
|
|
2014-12-30 13:57:45 +03:00
|
|
|
def _pack(self, fmt, *values):
|
2014-12-29 18:48:01 +03:00
|
|
|
return struct.pack(self._endian + fmt, *values)
|
|
|
|
|
|
|
|
def _register_loader(idx, size):
|
|
|
|
def decorator(func):
|
|
|
|
from PIL.TiffTags import TYPES
|
|
|
|
if func.__name__.startswith("load_"):
|
|
|
|
TYPES[idx] = func.__name__[5:].replace("_", " ")
|
|
|
|
_load_dispatch[idx] = size, func
|
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def _register_writer(idx):
|
|
|
|
def decorator(func):
|
|
|
|
_write_dispatch[idx] = func
|
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def _register_basic(idx_fmt_name):
|
|
|
|
from PIL.TiffTags import TYPES
|
|
|
|
idx, fmt, name = idx_fmt_name
|
|
|
|
TYPES[idx] = name
|
|
|
|
size = struct.calcsize("=" + fmt)
|
2015-09-13 13:07:43 +03:00
|
|
|
_load_dispatch[idx] = size, lambda self, legacy_api, data: (
|
2015-01-14 10:07:41 +03:00
|
|
|
self._unpack("{0}{1}".format(len(data) // size, fmt), data))
|
2014-12-29 18:48:01 +03:00
|
|
|
_write_dispatch[idx] = lambda self, *values: (
|
2014-12-30 13:57:45 +03:00
|
|
|
b"".join(self._pack(fmt, value) for value in values))
|
2014-12-29 18:48:01 +03:00
|
|
|
|
|
|
|
list(map(_register_basic,
|
2015-03-01 06:44:38 +03:00
|
|
|
[(3, "H", "short"), (4, "L", "long"),
|
2014-12-29 18:48:01 +03:00
|
|
|
(6, "b", "signed byte"), (8, "h", "signed short"),
|
|
|
|
(9, "l", "signed long"), (11, "f", "float"), (12, "d", "double")]))
|
|
|
|
|
2015-03-01 06:44:38 +03:00
|
|
|
@_register_loader(1, 1) # Basic type, except for the legacy API.
|
2015-09-13 13:07:43 +03:00
|
|
|
def load_byte(self, legacy_api, data):
|
|
|
|
return (data if legacy_api else
|
2015-03-01 06:44:38 +03:00
|
|
|
tuple(map(ord, data) if bytes is str else data))
|
|
|
|
|
|
|
|
@_register_writer(1) # Basic type, except for the legacy API.
|
|
|
|
def write_byte(self, data):
|
|
|
|
return data
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_loader(2, 1)
|
2015-09-13 13:07:43 +03:00
|
|
|
def load_string(self, legacy_api, data):
|
2014-12-29 18:48:01 +03:00
|
|
|
if data.endswith(b"\0"):
|
2010-07-31 06:52:47 +04:00
|
|
|
data = data[:-1]
|
2014-12-29 18:48:01 +03:00
|
|
|
return data.decode("latin-1", "replace")
|
|
|
|
|
|
|
|
@_register_writer(2)
|
|
|
|
def write_string(self, value):
|
|
|
|
# remerge of https://github.com/python-pillow/Pillow/pull/1416
|
|
|
|
if sys.version_info[0] == 2:
|
|
|
|
value = value.decode('ascii', 'replace')
|
|
|
|
return b"" + value.encode('ascii', 'replace') + b"\0"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_loader(5, 8)
|
2015-09-13 13:07:43 +03:00
|
|
|
def load_rational(self, legacy_api, data):
|
2015-01-14 10:07:41 +03:00
|
|
|
vals = self._unpack("{0}L".format(len(data) // 4), data)
|
2015-09-13 13:07:43 +03:00
|
|
|
combine = lambda a, b: (a, b) if legacy_api else a / b
|
2015-03-01 06:44:38 +03:00
|
|
|
return tuple(combine(num, denom)
|
|
|
|
for num, denom in zip(vals[::2], vals[1::2]))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_writer(5)
|
|
|
|
def write_rational(self, *values):
|
2014-12-30 13:57:45 +03:00
|
|
|
return b"".join(self._pack("2L", *_limit_rational(frac, 2 ** 31))
|
2014-12-29 18:48:01 +03:00
|
|
|
for frac in values)
|
|
|
|
|
|
|
|
@_register_loader(7, 1)
|
2015-09-13 13:07:43 +03:00
|
|
|
def load_undefined(self, legacy_api, data):
|
2010-07-31 06:52:47 +04:00
|
|
|
return data
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_writer(7)
|
|
|
|
def write_undefined(self, value):
|
|
|
|
return value
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_loader(10, 8)
|
2015-09-13 13:07:43 +03:00
|
|
|
def load_signed_rational(self, legacy_api, data):
|
2015-01-14 10:07:41 +03:00
|
|
|
vals = self._unpack("{0}l".format(len(data) // 4), data)
|
2015-09-13 13:07:43 +03:00
|
|
|
combine = lambda a, b: (a, b) if legacy_api else a / b
|
2015-03-01 06:44:38 +03:00
|
|
|
return tuple(combine(num, denom)
|
|
|
|
for num, denom in zip(vals[::2], vals[1::2]))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
@_register_writer(10)
|
|
|
|
def write_signed_rational(self, *values):
|
2014-12-30 13:57:45 +03:00
|
|
|
return b"".join(self._pack("2L", *_limit_rational(frac, 2 ** 30))
|
2014-12-29 18:48:01 +03:00
|
|
|
for frac in values)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:45:10 +03:00
|
|
|
def _ensure_read(self, fp, size):
|
|
|
|
ret = fp.read(size)
|
|
|
|
if len(ret) != size:
|
|
|
|
raise IOError("Corrupt EXIF data. " +
|
|
|
|
"Expecting to read %d bytes but only got %d. " %
|
|
|
|
(size, len(ret)))
|
|
|
|
return ret
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def load(self, fp):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
self.reset()
|
|
|
|
self._offset = fp.tell()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:45:10 +03:00
|
|
|
try:
|
|
|
|
for i in range(self._unpack("H", self._ensure_read(fp,2))[0]):
|
|
|
|
tag, typ, count, data = self._unpack("HHL4s", self._ensure_read(fp,12))
|
2015-09-10 18:33:14 +03:00
|
|
|
if DEBUG:
|
2015-09-13 17:15:13 +03:00
|
|
|
tagname = TAGS_V2.get(tag, TagInfo()).name
|
2015-09-11 20:45:10 +03:00
|
|
|
typname = TYPES.get(typ, "unknown")
|
|
|
|
print("tag: %s (%d) - type: %s (%d)" %
|
|
|
|
(tagname, tag, typname, typ), end=" ")
|
|
|
|
|
|
|
|
try:
|
|
|
|
unit_size, handler = self._load_dispatch[typ]
|
|
|
|
except KeyError:
|
|
|
|
if DEBUG:
|
|
|
|
print("- unsupported type", typ)
|
|
|
|
continue # ignore unsupported type
|
|
|
|
size = count * unit_size
|
|
|
|
if size > 4:
|
|
|
|
here = fp.tell()
|
|
|
|
offset, = self._unpack("L", data)
|
|
|
|
if DEBUG:
|
|
|
|
print("Tag Location: %s - Data Location: %s" %
|
|
|
|
(here, offset), end=" ")
|
|
|
|
fp.seek(offset)
|
|
|
|
data = ImageFile._safe_read(fp, size)
|
|
|
|
fp.seek(here)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2015-09-11 20:45:10 +03:00
|
|
|
data = data[:size]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:45:10 +03:00
|
|
|
if len(data) != size:
|
|
|
|
warnings.warn("Possibly corrupt EXIF data. "
|
|
|
|
"Expecting to read %d bytes but only got %d. "
|
|
|
|
"Skipping tag %s" % (size, len(data), tag))
|
|
|
|
continue
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:45:10 +03:00
|
|
|
self._tagdata[tag] = data
|
|
|
|
self.tagtype[tag] = typ
|
|
|
|
|
|
|
|
if DEBUG:
|
|
|
|
if size > 32:
|
|
|
|
print("- value: <table: %d bytes>" % size)
|
|
|
|
else:
|
|
|
|
print("- value:", self[tag])
|
|
|
|
|
|
|
|
self.next, = self._unpack("L", self._ensure_read(fp,4))
|
|
|
|
except IOError as msg:
|
|
|
|
warnings.warn(str(msg))
|
|
|
|
return
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def save(self, fp):
|
|
|
|
|
2014-12-30 13:57:45 +03:00
|
|
|
if fp.tell() == 0: # skip TIFF header on subsequent pages
|
|
|
|
# tiff header -- PIL always starts the first IFD at offset 8
|
|
|
|
fp.write(self._prefix + self._pack("HL", 42, 8))
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
# FIXME What about tagdata?
|
2015-09-13 16:01:01 +03:00
|
|
|
fp.write(self._pack("H", len(self._tags_v2)))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
entries = []
|
2015-09-13 16:01:01 +03:00
|
|
|
offset = fp.tell() + len(self._tags_v2) * 12 + 4
|
2010-07-31 06:52:47 +04:00
|
|
|
stripoffsets = None
|
|
|
|
|
|
|
|
# pass 1: convert tags to binary format
|
2014-12-29 18:48:01 +03:00
|
|
|
# always write tags in ascending order
|
2015-09-13 16:01:01 +03:00
|
|
|
for tag, value in sorted(self._tags_v2.items()):
|
2014-12-29 18:48:01 +03:00
|
|
|
if tag == STRIPOFFSETS:
|
|
|
|
stripoffsets = len(entries)
|
|
|
|
typ = self.tagtype.get(tag)
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("Tag %s, Type: %s, Value: %s" % (tag, typ, value))
|
2014-12-29 18:48:01 +03:00
|
|
|
values = value if isinstance(value, tuple) else (value,)
|
|
|
|
data = self._write_dispatch[typ](self, *values)
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-09-13 17:15:13 +03:00
|
|
|
tagname = TAGS_V2.get(tag, TagInfo()).name
|
2014-12-29 18:48:01 +03:00
|
|
|
typname = TYPES.get(typ, "unknown")
|
|
|
|
print("save: %s (%d) - type: %s (%d)" %
|
|
|
|
(tagname, tag, typname, typ), end=" ")
|
|
|
|
if len(data) >= 16:
|
|
|
|
print("- value: <table: %d bytes>" % len(data))
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2014-12-29 18:48:01 +03:00
|
|
|
print("- value:", values)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
# count is sum of lengths for string and arbitrary data
|
|
|
|
count = len(data) if typ in [2, 7] else len(values)
|
|
|
|
# figure out if data fits into the entry
|
|
|
|
if len(data) <= 4:
|
|
|
|
entries.append((tag, typ, count, data.ljust(4, b"\0"), b""))
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2014-12-30 13:57:45 +03:00
|
|
|
entries.append((tag, typ, count, self._pack("L", offset), data))
|
2014-12-29 18:48:01 +03:00
|
|
|
offset += (len(data) + 1) // 2 * 2 # pad to word
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# update strip offset data to point beyond auxiliary data
|
|
|
|
if stripoffsets is not None:
|
2014-12-29 18:48:01 +03:00
|
|
|
tag, typ, count, value, data = entries[stripoffsets]
|
|
|
|
if data:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"multistrip support not yet implemented")
|
2014-12-30 13:57:45 +03:00
|
|
|
value = self._pack("L", self._unpack("L", value)[0] + offset)
|
2014-12-29 18:48:01 +03:00
|
|
|
entries[stripoffsets] = tag, typ, count, value, data
|
|
|
|
|
|
|
|
# pass 2: write entries to file
|
|
|
|
for tag, typ, count, value, data in entries:
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG > 1:
|
2012-10-16 06:27:35 +04:00
|
|
|
print(tag, typ, count, repr(value), repr(data))
|
2014-12-30 13:57:45 +03:00
|
|
|
fp.write(self._pack("HHL4s", tag, typ, count, value))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# -- overwrite here for multi-page --
|
2014-12-29 18:48:01 +03:00
|
|
|
fp.write(b"\0\0\0\0") # end of entries
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# pass 3: write auxiliary data to file
|
2014-12-29 18:48:01 +03:00
|
|
|
for tag, typ, count, value, data in entries:
|
2010-07-31 06:52:47 +04:00
|
|
|
fp.write(data)
|
|
|
|
if len(data) & 1:
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
fp.write(b"\0")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return offset
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
ImageFileDirectory_v2._load_dispatch = _load_dispatch
|
|
|
|
ImageFileDirectory_v2._write_dispatch = _write_dispatch
|
2014-12-29 18:48:01 +03:00
|
|
|
for idx, name in TYPES.items():
|
|
|
|
name = name.replace(" ", "_")
|
2015-09-11 20:09:14 +03:00
|
|
|
setattr(ImageFileDirectory_v2, "load_" + name, _load_dispatch[idx][1])
|
|
|
|
setattr(ImageFileDirectory_v2, "write_" + name, _write_dispatch[idx])
|
2014-12-29 18:48:01 +03:00
|
|
|
del _load_dispatch, _write_dispatch, idx, name
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
#Legacy ImageFileDirectory support.
|
|
|
|
class ImageFileDirectory_v1(ImageFileDirectory_v2):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
ImageFileDirectory_v2.__init__(self, *args, **kwargs)
|
2015-09-13 16:01:01 +03:00
|
|
|
self._legacy_api=True
|
2015-09-11 20:09:14 +03:00
|
|
|
#insert deprecation warning here.
|
|
|
|
|
2015-09-13 16:01:01 +03:00
|
|
|
tags = property(lambda self: self._tags_v1)
|
2015-09-11 20:09:14 +03:00
|
|
|
tagdata = property(lambda self: self._tagdata)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_v2(cls, original):
|
2015-09-13 16:01:01 +03:00
|
|
|
""" returns: ImageFileDirectory_v1
|
|
|
|
|
|
|
|
Returns an ImageFileDirectory_v1 instance with the same
|
|
|
|
data as is contained in the original ImageFileDirectory_v2
|
|
|
|
instance """
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = cls(prefix=original.prefix)
|
|
|
|
ifd._tagdata = original._tagdata
|
|
|
|
ifd.tagtype = original.tagtype
|
|
|
|
return ifd
|
2015-09-13 16:01:01 +03:00
|
|
|
|
|
|
|
def to_v2(self):
|
|
|
|
""" returns: ImageFileDirectory_v2
|
|
|
|
|
|
|
|
Returns an ImageFileDirectory_v2 instance with the same
|
|
|
|
data as is contained in this ImageFileDirectory_v1 instance """
|
|
|
|
|
|
|
|
ifd = ImageFileDirectory_v2(prefix=self.prefix)
|
|
|
|
ifd._tagdata = dict(self._tagdata)
|
|
|
|
ifd.tagtype = dict(self.tagtype)
|
|
|
|
ifd._tags_v2 = dict(self._tags_v2)
|
|
|
|
return ifd
|
|
|
|
|
|
|
|
def __contains__(self, tag):
|
|
|
|
return tag in self._tags_v1 or tag in self._tagdata
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(set(self._tagdata) | set(self._tags_v1))
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(set(self._tagdata) | set(self._tags_v1))
|
|
|
|
|
|
|
|
def __setitem__(self, tag, value):
|
|
|
|
for legacy_api in (False,True):
|
|
|
|
self._setitem(tag, value, legacy_api)
|
|
|
|
|
|
|
|
def __getitem__(self, tag):
|
|
|
|
if tag not in self._tags_v1: # unpack on the fly
|
|
|
|
data = self._tagdata[tag]
|
|
|
|
typ = self.tagtype[tag]
|
|
|
|
size, handler = self._load_dispatch[typ]
|
|
|
|
for legacy in (False, True):
|
|
|
|
self._setitem(tag, handler(self, legacy, data), legacy)
|
|
|
|
val = self._tags_v1[tag]
|
|
|
|
if not isinstance(val, (tuple, bytes)):
|
|
|
|
val = val,
|
|
|
|
return val
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
|
|
|
|
# undone -- switch this pointer when IFD_LEGACY_API == False
|
|
|
|
ImageFileDirectory = ImageFileDirectory_v1
|
2014-07-28 20:00:06 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for TIFF files.
|
|
|
|
|
|
|
|
class TiffImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "TIFF"
|
|
|
|
format_description = "Adobe TIFF"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
"Open the first image in a TIFF file"
|
|
|
|
|
|
|
|
# Header
|
|
|
|
ifh = self.fp.read(8)
|
|
|
|
|
|
|
|
# image file directory (tag dictionary)
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tag_v2 = ImageFileDirectory_v2(ifh)
|
|
|
|
|
|
|
|
# legacy tag/ifd entries will be filled in later
|
|
|
|
self.tag = self.ifd = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# setup frame pointers
|
2015-09-11 20:09:14 +03:00
|
|
|
self.__first = self.__next = self.tag_v2.next
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__frame = -1
|
|
|
|
self.__fp = self.fp
|
2015-04-15 03:43:05 +03:00
|
|
|
self._frame_pos = []
|
|
|
|
self._n_frames = None
|
2015-06-30 06:25:00 +03:00
|
|
|
self._is_animated = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("*** TiffImageFile._open ***")
|
|
|
|
print("- __first:", self.__first)
|
|
|
|
print("- ifh: ", ifh)
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
# and load the first frame
|
2010-07-31 06:52:47 +04:00
|
|
|
self._seek(0)
|
|
|
|
|
2015-04-15 03:43:05 +03:00
|
|
|
@property
|
|
|
|
def n_frames(self):
|
|
|
|
if self._n_frames is None:
|
|
|
|
current = self.tell()
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
self._seek(self.tell() + 1)
|
|
|
|
except EOFError:
|
|
|
|
self._n_frames = self.tell() + 1
|
|
|
|
self.seek(current)
|
|
|
|
return self._n_frames
|
|
|
|
|
2015-06-30 06:25:00 +03:00
|
|
|
@property
|
|
|
|
def is_animated(self):
|
|
|
|
if self._is_animated is None:
|
|
|
|
current = self.tell()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.seek(1)
|
|
|
|
self._is_animated = True
|
|
|
|
except EOFError:
|
|
|
|
self._is_animated = False
|
|
|
|
|
|
|
|
self.seek(current)
|
|
|
|
return self._is_animated
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def seek(self, frame):
|
|
|
|
"Select a given frame as current image"
|
2015-04-23 11:06:32 +03:00
|
|
|
self._seek(max(frame, 0)) # Questionable backwards compatibility.
|
2014-08-27 02:30:03 +04:00
|
|
|
# Create a new core image object on second and
|
|
|
|
# subsequent frames in the image. Image may be
|
|
|
|
# different size/mode.
|
|
|
|
Image._decompression_bomb_check(self.size)
|
|
|
|
self.im = Image.core.new(self.mode, self.size)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def _seek(self, frame):
|
|
|
|
self.fp = self.__fp
|
2015-04-15 03:43:05 +03:00
|
|
|
while len(self._frame_pos) <= frame:
|
2010-07-31 06:52:47 +04:00
|
|
|
if not self.__next:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise EOFError("no more images in TIFF file")
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-28 11:56:48 +03:00
|
|
|
print("Seeking to frame %s, on frame %s, "
|
2015-08-25 15:27:18 +03:00
|
|
|
"__next %s, location: %s" %
|
2014-10-02 11:43:22 +04:00
|
|
|
(frame, self.__frame, self.__next, self.fp.tell()))
|
2014-09-03 02:08:51 +04:00
|
|
|
# reset python3 buffered io handle in case fp
|
|
|
|
# was passed to libtiff, invalidating the buffer
|
|
|
|
self.fp.tell()
|
2010-07-31 06:52:47 +04:00
|
|
|
self.fp.seek(self.__next)
|
2015-04-15 03:43:05 +03:00
|
|
|
self._frame_pos.append(self.__next)
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2014-10-02 11:45:41 +04:00
|
|
|
print("Loading tags, location: %s" % self.fp.tell())
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tag_v2.load(self.fp)
|
|
|
|
self.__next = self.tag_v2.next
|
2014-05-10 08:36:15 +04:00
|
|
|
self.__frame += 1
|
2015-04-15 03:43:05 +03:00
|
|
|
self.fp.seek(self._frame_pos[frame])
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tag_v2.load(self.fp)
|
|
|
|
# fill the legacy tag/ifd entries
|
|
|
|
self.tag = self.ifd = ImageFileDirectory_v1.from_v2(self.tag_v2)
|
2015-04-15 03:43:05 +03:00
|
|
|
self.__frame = frame
|
2010-07-31 06:52:47 +04:00
|
|
|
self._setup()
|
2014-10-02 11:43:22 +04:00
|
|
|
|
2015-04-15 03:43:05 +03:00
|
|
|
def tell(self):
|
|
|
|
"Return the current frame number"
|
2010-07-31 06:52:47 +04:00
|
|
|
return self.__frame
|
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
def _decoder(self, rawmode, layer, tile=None):
|
2010-07-31 06:52:47 +04:00
|
|
|
"Setup decoder contexts"
|
|
|
|
|
|
|
|
args = None
|
|
|
|
if rawmode == "RGB" and self._planar_configuration == 2:
|
|
|
|
rawmode = rawmode[layer]
|
|
|
|
compression = self._compression
|
|
|
|
if compression == "raw":
|
|
|
|
args = (rawmode, 0, 1)
|
|
|
|
elif compression == "jpeg":
|
|
|
|
args = rawmode, ""
|
2015-09-11 20:09:14 +03:00
|
|
|
if JPEGTABLES in self.tag_v2:
|
2010-07-31 06:52:47 +04:00
|
|
|
# Hack to handle abbreviated JPEG headers
|
2014-12-29 18:48:01 +03:00
|
|
|
# FIXME This will fail with more than one value
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tile_prefix, = self.tag_v2[JPEGTABLES]
|
2010-07-31 06:52:47 +04:00
|
|
|
elif compression == "packbits":
|
|
|
|
args = rawmode
|
|
|
|
elif compression == "tiff_lzw":
|
|
|
|
args = rawmode
|
2015-09-11 20:09:14 +03:00
|
|
|
if PREDICTOR in self.tag_v2:
|
2010-07-31 06:52:47 +04:00
|
|
|
# Section 14: Differencing Predictor
|
2015-09-11 20:09:14 +03:00
|
|
|
self.decoderconfig = (self.tag_v2[PREDICTOR],)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
if ICCPROFILE in self.tag_v2:
|
|
|
|
self.info['icc_profile'] = self.tag_v2[ICCPROFILE]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return args
|
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
def _load_libtiff(self):
|
2013-11-22 08:57:48 +04:00
|
|
|
""" Overload method triggered when we detect a compressed tiff
|
|
|
|
Calls out to libtiff """
|
2013-03-09 07:51:59 +04:00
|
|
|
|
|
|
|
pixel = Image.Image.load(self)
|
|
|
|
|
|
|
|
if self.tile is None:
|
|
|
|
raise IOError("cannot load this image")
|
|
|
|
if not self.tile:
|
|
|
|
return pixel
|
|
|
|
|
|
|
|
self.load_prepare()
|
2013-05-08 00:23:51 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
if not len(self.tile) == 1:
|
|
|
|
raise IOError("Not exactly one tile")
|
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
# (self._compression, (extents tuple),
|
|
|
|
# 0, (rawmode, self._compression, fp))
|
2015-04-08 14:12:37 +03:00
|
|
|
extents = self.tile[0][1]
|
2015-09-11 20:09:14 +03:00
|
|
|
args = self.tile[0][3] + (self.tag_v2.offset,)
|
2014-07-30 00:00:38 +04:00
|
|
|
decoder = Image._getdecoder(self.mode, 'libtiff', args,
|
|
|
|
self.decoderconfig)
|
2013-03-09 07:51:59 +04:00
|
|
|
try:
|
2014-03-28 09:19:39 +04:00
|
|
|
decoder.setimage(self.im, extents)
|
2013-03-09 07:51:59 +04:00
|
|
|
except ValueError:
|
|
|
|
raise IOError("Couldn't set the image")
|
|
|
|
|
2013-03-14 21:36:15 +04:00
|
|
|
if hasattr(self.fp, "getvalue"):
|
2013-03-09 07:51:59 +04:00
|
|
|
# We've got a stringio like thing passed in. Yay for all in memory.
|
|
|
|
# The decoder needs the entire file in one shot, so there's not
|
|
|
|
# a lot we can do here other than give it the entire file.
|
2014-07-28 20:00:06 +04:00
|
|
|
# unless we could do something like get the address of the
|
|
|
|
# underlying string for stringio.
|
2013-03-14 21:36:15 +04:00
|
|
|
#
|
|
|
|
# Rearranging for supporting byteio items, since they have a fileno
|
2014-07-28 20:00:06 +04:00
|
|
|
# that returns an IOError if there's no underlying fp. Easier to
|
2015-05-29 07:59:54 +03:00
|
|
|
# deal with here by reordering.
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("have getvalue. just sending in a string from getvalue")
|
2014-07-28 20:00:06 +04:00
|
|
|
n, err = decoder.decode(self.fp.getvalue())
|
2013-03-14 21:36:15 +04:00
|
|
|
elif hasattr(self.fp, "fileno"):
|
|
|
|
# we've got a actual file on disk, pass in the fp.
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("have fileno, calling fileno version of the decoder.")
|
2013-03-14 21:36:15 +04:00
|
|
|
self.fp.seek(0)
|
2014-07-28 20:00:06 +04:00
|
|
|
# 4 bytes, otherwise the trace might error out
|
|
|
|
n, err = decoder.decode(b"fpfp")
|
2013-03-09 07:51:59 +04:00
|
|
|
else:
|
|
|
|
# we have something else.
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("don't have fileno or getvalue. just reading")
|
2013-05-08 00:23:51 +04:00
|
|
|
# UNDONE -- so much for that buffer size thing.
|
2014-07-28 20:00:06 +04:00
|
|
|
n, err = decoder.decode(self.fp.read())
|
2013-05-08 00:23:51 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
self.tile = []
|
|
|
|
self.readonly = 0
|
2014-03-28 09:18:40 +04:00
|
|
|
# libtiff closed the fp in a, we need to close self.fp, if possible
|
|
|
|
if hasattr(self.fp, 'close'):
|
2014-08-21 08:44:28 +04:00
|
|
|
if not self.__next:
|
|
|
|
self.fp.close()
|
2014-07-28 20:00:06 +04:00
|
|
|
self.fp = None # might be shared
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2014-03-28 09:19:39 +04:00
|
|
|
if err < 0:
|
|
|
|
raise IOError(err)
|
2013-03-09 07:51:59 +04:00
|
|
|
|
|
|
|
self.load_end()
|
|
|
|
|
|
|
|
return Image.Image.load(self)
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _setup(self):
|
|
|
|
"Setup this image object based on current tags"
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
if 0xBC01 in self.tag_v2:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise IOError("Windows Media Photo files not yet supported")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# extract relevant tags
|
2015-09-11 20:09:14 +03:00
|
|
|
self._compression = COMPRESSION_INFO[self.tag_v2.get(COMPRESSION, 1)]
|
|
|
|
self._planar_configuration = self.tag_v2.get(PLANAR_CONFIGURATION, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# photometric is a required tag, but not everyone is reading
|
|
|
|
# the specification
|
2015-09-11 20:09:14 +03:00
|
|
|
photo = self.tag_v2.get(PHOTOMETRIC_INTERPRETATION, 0)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
fillorder = self.tag_v2.get(FILLORDER, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("*** Summary ***")
|
|
|
|
print("- compression:", self._compression)
|
|
|
|
print("- photometric_interpretation:", photo)
|
|
|
|
print("- planar_configuration:", self._planar_configuration)
|
|
|
|
print("- fill_order:", fillorder)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# size
|
2015-09-11 20:09:14 +03:00
|
|
|
xsize = self.tag_v2.get(IMAGEWIDTH)
|
|
|
|
ysize = self.tag_v2.get(IMAGELENGTH)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.size = xsize, ysize
|
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("- size:", self.size)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
format = self.tag_v2.get(SAMPLEFORMAT, (1,))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# mode: check photometric interpretation and bits per pixel
|
|
|
|
key = (
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tag_v2.prefix, photo, format, fillorder,
|
|
|
|
self.tag_v2.get(BITSPERSAMPLE, (1,)),
|
|
|
|
self.tag_v2.get(EXTRASAMPLES, ())
|
2010-07-31 06:52:47 +04:00
|
|
|
)
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("format key:", key)
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
self.mode, rawmode = OPEN_INFO[key]
|
|
|
|
except KeyError:
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("- unsupported format")
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("unknown pixel mode")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("- raw mode:", rawmode)
|
|
|
|
print("- pil mode:", self.mode)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.info["compression"] = self._compression
|
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
xres = self.tag_v2.get(X_RESOLUTION, (1, 1))
|
|
|
|
yres = self.tag_v2.get(Y_RESOLUTION, (1, 1))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-05-08 00:33:21 +04:00
|
|
|
if xres and not isinstance(xres, tuple):
|
|
|
|
xres = (xres, 1.)
|
|
|
|
if yres and not isinstance(yres, tuple):
|
|
|
|
yres = (yres, 1.)
|
2010-07-31 06:52:47 +04:00
|
|
|
if xres and yres:
|
|
|
|
xres = xres[0] / (xres[1] or 1)
|
|
|
|
yres = yres[0] / (yres[1] or 1)
|
2015-09-11 20:09:14 +03:00
|
|
|
resunit = self.tag_v2.get(RESOLUTION_UNIT, 1)
|
2014-07-28 20:00:06 +04:00
|
|
|
if resunit == 2: # dots per inch
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["dpi"] = xres, yres
|
2014-07-28 20:00:06 +04:00
|
|
|
elif resunit == 3: # dots per centimeter. convert to dpi
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["dpi"] = xres * 2.54, yres * 2.54
|
2014-07-28 20:00:06 +04:00
|
|
|
else: # No absolute unit of measurement
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["resolution"] = xres, yres
|
|
|
|
|
|
|
|
# build tile descriptors
|
|
|
|
x = y = l = 0
|
|
|
|
self.tile = []
|
2015-09-11 20:09:14 +03:00
|
|
|
if STRIPOFFSETS in self.tag_v2:
|
2010-07-31 06:52:47 +04:00
|
|
|
# striped image
|
2015-09-11 20:09:14 +03:00
|
|
|
offsets = self.tag_v2[STRIPOFFSETS]
|
|
|
|
h = self.tag_v2.get(ROWSPERSTRIP, ysize)
|
2010-07-31 06:52:47 +04:00
|
|
|
w = self.size[0]
|
2014-07-30 00:00:38 +04:00
|
|
|
if READ_LIBTIFF or self._compression in ["tiff_ccitt", "group3",
|
|
|
|
"group4", "tiff_jpeg",
|
|
|
|
"tiff_adobe_deflate",
|
|
|
|
"tiff_thunderscan",
|
|
|
|
"tiff_deflate",
|
|
|
|
"tiff_sgilog",
|
|
|
|
"tiff_sgilog24",
|
|
|
|
"tiff_raw_16"]:
|
2015-02-06 21:58:07 +03:00
|
|
|
# if DEBUG:
|
2014-07-28 20:00:06 +04:00
|
|
|
# print "Activating g4 compression for whole file"
|
2013-03-09 07:51:59 +04:00
|
|
|
|
|
|
|
# Decoder expects entire file as one tile.
|
|
|
|
# There's a buffer size limit in load (64k)
|
|
|
|
# so large g4 images will fail if we use that
|
2013-05-08 00:23:51 +04:00
|
|
|
# function.
|
2013-03-09 07:51:59 +04:00
|
|
|
#
|
|
|
|
# Setup the one tile for the whole image, then
|
|
|
|
# replace the existing load function with our
|
|
|
|
# _load_libtiff function.
|
2013-05-08 00:23:51 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
self.load = self._load_libtiff
|
2013-05-08 00:23:51 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
# To be nice on memory footprint, if there's a
|
|
|
|
# file descriptor, use that instead of reading
|
|
|
|
# into a string in python.
|
|
|
|
|
2013-05-08 00:23:51 +04:00
|
|
|
# libtiff closes the file descriptor, so pass in a dup.
|
2013-03-14 21:36:15 +04:00
|
|
|
try:
|
2014-07-28 20:00:06 +04:00
|
|
|
fp = hasattr(self.fp, "fileno") and \
|
|
|
|
os.dup(self.fp.fileno())
|
2014-10-13 22:12:14 +04:00
|
|
|
# flush the file descriptor, prevents error on pypy 2.4+
|
|
|
|
# should also eliminate the need for fp.tell for py3
|
|
|
|
# in _seek
|
|
|
|
self.fp.flush()
|
2013-03-14 21:36:15 +04:00
|
|
|
except IOError:
|
|
|
|
# io.BytesIO have a fileno, but returns an IOError if
|
|
|
|
# it doesn't use a file descriptor.
|
|
|
|
fp = False
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2013-07-11 09:36:55 +04:00
|
|
|
# libtiff handles the fillmode for us, so 1;IR should
|
|
|
|
# actually be 1;I. Including the R double reverses the
|
|
|
|
# bits, so stripes of the image are reversed. See
|
2014-06-03 14:02:44 +04:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/279
|
2013-07-11 09:36:55 +04:00
|
|
|
if fillorder == 2:
|
|
|
|
key = (
|
2015-09-11 20:09:14 +03:00
|
|
|
self.tag_v2.prefix, photo, format, 1,
|
|
|
|
self.tag_v2.get(BITSPERSAMPLE, (1,)),
|
|
|
|
self.tag_v2.get(EXTRASAMPLES, ())
|
2013-07-11 09:36:55 +04:00
|
|
|
)
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2013-07-11 09:36:55 +04:00
|
|
|
print("format key:", key)
|
|
|
|
# this should always work, since all the
|
|
|
|
# fillorder==2 modes have a corresponding
|
|
|
|
# fillorder=1 mode
|
|
|
|
self.mode, rawmode = OPEN_INFO[key]
|
2013-10-22 02:17:33 +04:00
|
|
|
# libtiff always returns the bytes in native order.
|
|
|
|
# we're expecting image byte order. So, if the rawmode
|
|
|
|
# contains I;16, we need to convert from native to image
|
|
|
|
# byte order.
|
2013-11-22 08:14:06 +04:00
|
|
|
if self.mode in ('I;16B', 'I;16') and 'I;16' in rawmode:
|
2013-10-22 02:17:33 +04:00
|
|
|
rawmode = 'I;16N'
|
2013-07-11 09:36:55 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
# Offset in the tile tuple is 0, we go from 0,0 to
|
|
|
|
# w,h, and we only do this once -- eds
|
2014-07-28 20:00:06 +04:00
|
|
|
a = (rawmode, self._compression, fp)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.tile.append(
|
|
|
|
(self._compression,
|
2013-03-09 07:51:59 +04:00
|
|
|
(0, 0, w, ysize),
|
|
|
|
0, a))
|
|
|
|
a = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
for i in range(len(offsets)):
|
|
|
|
a = self._decoder(rawmode, l, i)
|
|
|
|
self.tile.append(
|
|
|
|
(self._compression,
|
2014-07-28 20:00:06 +04:00
|
|
|
(0, min(y, ysize), w, min(y+h, ysize)),
|
|
|
|
offsets[i], a))
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("tiles: ", self.tile)
|
2013-03-09 07:51:59 +04:00
|
|
|
y = y + h
|
|
|
|
if y >= self.size[1]:
|
|
|
|
x = y = 0
|
2014-05-10 08:36:15 +04:00
|
|
|
l += 1
|
2010-07-31 06:52:47 +04:00
|
|
|
a = None
|
2015-09-11 20:09:14 +03:00
|
|
|
elif TILEOFFSETS in self.tag_v2:
|
2010-07-31 06:52:47 +04:00
|
|
|
# tiled image
|
2015-09-11 20:09:14 +03:00
|
|
|
w = self.tag_v2.get(322)
|
|
|
|
h = self.tag_v2.get(323)
|
2010-07-31 06:52:47 +04:00
|
|
|
a = None
|
2015-09-11 20:09:14 +03:00
|
|
|
for o in self.tag_v2[TILEOFFSETS]:
|
2010-07-31 06:52:47 +04:00
|
|
|
if not a:
|
|
|
|
a = self._decoder(rawmode, l)
|
|
|
|
# FIXME: this doesn't work if the image size
|
|
|
|
# is not a multiple of the tile size...
|
|
|
|
self.tile.append(
|
|
|
|
(self._compression,
|
2014-07-28 20:00:06 +04:00
|
|
|
(x, y, x+w, y+h),
|
|
|
|
o, a))
|
2010-07-31 06:52:47 +04:00
|
|
|
x = x + w
|
|
|
|
if x >= self.size[0]:
|
|
|
|
x, y = 0, y + h
|
|
|
|
if y >= self.size[1]:
|
|
|
|
x = y = 0
|
2014-05-10 08:36:15 +04:00
|
|
|
l += 1
|
2010-07-31 06:52:47 +04:00
|
|
|
a = None
|
|
|
|
else:
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("- unsupported data organization")
|
2010-07-31 06:52:47 +04:00
|
|
|
raise SyntaxError("unknown data organization")
|
|
|
|
|
|
|
|
# fixup palette descriptor
|
|
|
|
|
|
|
|
if self.mode == "P":
|
2015-09-11 20:09:14 +03:00
|
|
|
palette = [o8(b // 256) for b in self.tag_v2[COLORMAP]]
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
self.palette = ImagePalette.raw("RGB;L", b"".join(palette))
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write TIFF files
|
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
# little endian is default except for image modes with
|
2015-05-29 07:59:54 +03:00
|
|
|
# explicit big endian byte-order
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
SAVE_INFO = {
|
2014-07-28 20:00:06 +04:00
|
|
|
# mode => rawmode, byteorder, photometrics,
|
|
|
|
# sampleformat, bitspersample, extra
|
2010-07-31 06:52:47 +04:00
|
|
|
"1": ("1", II, 1, 1, (1,), None),
|
|
|
|
"L": ("L", II, 1, 1, (8,), None),
|
2014-07-28 20:00:06 +04:00
|
|
|
"LA": ("LA", II, 1, 1, (8, 8), 2),
|
2010-07-31 06:52:47 +04:00
|
|
|
"P": ("P", II, 3, 1, (8,), None),
|
2014-07-28 20:00:06 +04:00
|
|
|
"PA": ("PA", II, 3, 1, (8, 8), 2),
|
2010-07-31 06:52:47 +04:00
|
|
|
"I": ("I;32S", II, 1, 2, (32,), None),
|
|
|
|
"I;16": ("I;16", II, 1, 1, (16,), None),
|
|
|
|
"I;16S": ("I;16S", II, 1, 2, (16,), None),
|
|
|
|
"F": ("F;32F", II, 1, 3, (32,), None),
|
2014-07-28 20:00:06 +04:00
|
|
|
"RGB": ("RGB", II, 2, 1, (8, 8, 8), None),
|
|
|
|
"RGBX": ("RGBX", II, 2, 1, (8, 8, 8, 8), 0),
|
|
|
|
"RGBA": ("RGBA", II, 2, 1, (8, 8, 8, 8), 2),
|
|
|
|
"CMYK": ("CMYK", II, 5, 1, (8, 8, 8, 8), None),
|
|
|
|
"YCbCr": ("YCbCr", II, 6, 1, (8, 8, 8), None),
|
|
|
|
"LAB": ("LAB", II, 8, 1, (8, 8, 8), None),
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
"I;32BS": ("I;32BS", MM, 1, 2, (32,), None),
|
|
|
|
"I;16B": ("I;16B", MM, 1, 1, (16,), None),
|
|
|
|
"I;16BS": ("I;16BS", MM, 1, 2, (16,), None),
|
|
|
|
"F;32BF": ("F;32BF", MM, 1, 3, (32,), None),
|
|
|
|
}
|
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _save(im, fp, filename):
|
|
|
|
|
|
|
|
try:
|
|
|
|
rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode]
|
|
|
|
except KeyError:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise IOError("cannot write mode %s as TIFF" % im.mode)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = ImageFileDirectory_v2(prefix=prefix)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-13 16:16:12 +03:00
|
|
|
compression = im.encoderinfo.get('compression',
|
|
|
|
im.info.get('compression', 'raw'))
|
2013-11-22 09:41:54 +04:00
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
libtiff = WRITE_LIBTIFF or compression != 'raw'
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2013-11-06 08:49:09 +04:00
|
|
|
# required for color libtiff images
|
|
|
|
ifd[PLANAR_CONFIGURATION] = getattr(im, '_planar_configuration', 1)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
ifd[IMAGEWIDTH] = im.size[0]
|
|
|
|
ifd[IMAGELENGTH] = im.size[1]
|
|
|
|
|
2013-10-03 09:06:17 +04:00
|
|
|
# write any arbitrary tags passed in as an ImageFileDirectory
|
2014-07-28 20:00:06 +04:00
|
|
|
info = im.encoderinfo.get("tiffinfo", {})
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2014-12-29 18:48:01 +03:00
|
|
|
print("Tiffinfo Keys: %s" % list(info))
|
2015-09-13 16:01:01 +03:00
|
|
|
if isinstance(info, ImageFileDirectory_v1):
|
|
|
|
info = info.to_v2()
|
2014-12-29 18:48:01 +03:00
|
|
|
for key in info:
|
2013-10-03 09:06:17 +04:00
|
|
|
ifd[key] = info.get(key)
|
|
|
|
try:
|
|
|
|
ifd.tagtype[key] = info.tagtype[key]
|
|
|
|
except:
|
2014-07-28 20:00:06 +04:00
|
|
|
pass # might not be an IFD, Might not have populated type
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# additions written by Greg Couch, gregc@cgl.ucsf.edu
|
|
|
|
# inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
|
2015-09-13 12:53:47 +03:00
|
|
|
if hasattr(im, 'tag_v2'):
|
2010-07-31 06:52:47 +04:00
|
|
|
# preserve tags from original TIFF image file
|
2013-10-08 04:00:20 +04:00
|
|
|
for key in (RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION,
|
|
|
|
IPTC_NAA_CHUNK, PHOTOSHOP_CHUNK, XMP):
|
2015-09-13 12:53:47 +03:00
|
|
|
if key in im.tag_v2:
|
|
|
|
ifd[key] = im.tag_v2[key]
|
|
|
|
ifd.tagtype[key] = im.tag_v2.tagtype.get(key, None)
|
2013-10-08 04:00:20 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# preserve ICC profile (should also work when saving other formats
|
|
|
|
# which support profiles as TIFF) -- 2008-06-06 Florian Hoech
|
2012-10-16 01:18:27 +04:00
|
|
|
if "icc_profile" in im.info:
|
2010-07-31 06:52:47 +04:00
|
|
|
ifd[ICCPROFILE] = im.info["icc_profile"]
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
for key, name in [(IMAGEDESCRIPTION, "description"),
|
|
|
|
(X_RESOLUTION, "resolution"),
|
|
|
|
(Y_RESOLUTION, "resolution"),
|
|
|
|
(X_RESOLUTION, "x_resolution"),
|
|
|
|
(Y_RESOLUTION, "y_resolution"),
|
|
|
|
(RESOLUTION_UNIT, "resolution_unit"),
|
|
|
|
(SOFTWARE, "software"),
|
|
|
|
(DATE_TIME, "date_time"),
|
|
|
|
(ARTIST, "artist"),
|
|
|
|
(COPYRIGHT, "copyright")]:
|
2014-12-10 02:17:33 +03:00
|
|
|
name_with_spaces = name.replace("_", " ")
|
|
|
|
if "_" in name and name_with_spaces in im.encoderinfo:
|
|
|
|
warnings.warn("%r is deprecated; use %r instead" %
|
|
|
|
(name_with_spaces, name), DeprecationWarning)
|
2014-12-29 18:48:01 +03:00
|
|
|
ifd[key] = im.encoderinfo[name.replace("_", " ")]
|
2014-12-10 02:17:33 +03:00
|
|
|
if name in im.encoderinfo:
|
2014-12-29 18:48:01 +03:00
|
|
|
ifd[key] = im.encoderinfo[name]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
dpi = im.encoderinfo.get("dpi")
|
|
|
|
if dpi:
|
|
|
|
ifd[RESOLUTION_UNIT] = 2
|
2014-12-29 18:48:01 +03:00
|
|
|
ifd[X_RESOLUTION] = dpi[0]
|
|
|
|
ifd[Y_RESOLUTION] = dpi[1]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if bits != (1,):
|
|
|
|
ifd[BITSPERSAMPLE] = bits
|
|
|
|
if len(bits) != 1:
|
|
|
|
ifd[SAMPLESPERPIXEL] = len(bits)
|
|
|
|
if extra is not None:
|
|
|
|
ifd[EXTRASAMPLES] = extra
|
|
|
|
if format != 1:
|
|
|
|
ifd[SAMPLEFORMAT] = format
|
|
|
|
|
|
|
|
ifd[PHOTOMETRIC_INTERPRETATION] = photo
|
|
|
|
|
|
|
|
if im.mode == "P":
|
|
|
|
lut = im.im.getpalette("RGB", "RGB;L")
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
ifd[COLORMAP] = tuple(i8(v) * 256 for v in lut)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# data orientation
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
stride = len(bits) * ((im.size[0]*bits[0]+7)//8)
|
2010-07-31 06:52:47 +04:00
|
|
|
ifd[ROWSPERSTRIP] = im.size[1]
|
|
|
|
ifd[STRIPBYTECOUNTS] = stride * im.size[1]
|
2014-07-28 20:00:06 +04:00
|
|
|
ifd[STRIPOFFSETS] = 0 # this is adjusted by IFD writer
|
|
|
|
# no compression by default:
|
|
|
|
ifd[COMPRESSION] = COMPRESSION_INFO_REV.get(compression, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
if libtiff:
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2015-08-25 15:27:18 +03:00
|
|
|
print("Saving using libtiff encoder")
|
2014-12-29 18:48:01 +03:00
|
|
|
print("Items: %s" % sorted(ifd.items()))
|
2013-03-09 07:51:59 +04:00
|
|
|
_fp = 0
|
|
|
|
if hasattr(fp, "fileno"):
|
2014-11-14 15:29:10 +03:00
|
|
|
try:
|
|
|
|
fp.seek(0)
|
|
|
|
_fp = os.dup(fp.fileno())
|
|
|
|
except io.UnsupportedOperation:
|
|
|
|
pass
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2014-07-28 20:00:06 +04:00
|
|
|
# ICC Profile crashes.
|
|
|
|
blocklist = [STRIPOFFSETS, STRIPBYTECOUNTS, ROWSPERSTRIP, ICCPROFILE]
|
|
|
|
atts = {}
|
2014-06-03 14:02:44 +04:00
|
|
|
# bits per sample is a single short in the tiff directory, not a list.
|
2013-11-06 08:49:09 +04:00
|
|
|
atts[BITSPERSAMPLE] = bits[0]
|
2013-10-22 20:18:41 +04:00
|
|
|
# Merge the ones that we have with (optional) more bits from
|
|
|
|
# the original file, e.g x,y resolution so that we can
|
|
|
|
# save(load('')) == original file.
|
2014-07-30 00:00:38 +04:00
|
|
|
for k, v in itertools.chain(ifd.items(),
|
|
|
|
getattr(im, 'ifd', {}).items()):
|
2013-10-22 20:18:41 +04:00
|
|
|
if k not in atts and k not in blocklist:
|
2014-12-29 18:48:01 +03:00
|
|
|
if isinstance(v, unicode if bytes is str else str):
|
|
|
|
atts[k] = v.encode('ascii', 'replace') + b"\0"
|
2013-10-22 21:10:37 +04:00
|
|
|
else:
|
|
|
|
atts[k] = v
|
2013-10-21 20:30:06 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
if DEBUG:
|
2014-12-29 18:48:01 +03:00
|
|
|
print("Converted items: %s" % sorted(atts.items()))
|
2013-10-22 02:37:20 +04:00
|
|
|
|
2013-11-22 08:57:48 +04:00
|
|
|
# libtiff always expects the bytes in native order.
|
|
|
|
# we're storing image byte order. So, if the rawmode
|
2013-10-22 02:37:20 +04:00
|
|
|
# contains I;16, we need to convert from native to image
|
|
|
|
# byte order.
|
|
|
|
if im.mode in ('I;16B', 'I;16'):
|
|
|
|
rawmode = 'I;16N'
|
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
a = (rawmode, compression, _fp, filename, atts)
|
2015-08-25 15:27:18 +03:00
|
|
|
# print(im.mode, compression, a, im.encoderconfig)
|
2013-11-22 08:32:42 +04:00
|
|
|
e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
|
2014-07-28 20:00:06 +04:00
|
|
|
e.setimage(im.im, (0, 0)+im.size)
|
2014-01-08 06:21:24 +04:00
|
|
|
while True:
|
2014-07-28 20:00:06 +04:00
|
|
|
# undone, change to self.decodermaxblock:
|
|
|
|
l, s, d = e.encode(16*1024)
|
2013-03-09 07:51:59 +04:00
|
|
|
if not _fp:
|
|
|
|
fp.write(d)
|
|
|
|
if s:
|
|
|
|
break
|
|
|
|
if s < 0:
|
|
|
|
raise IOError("encoder error %d when writing image file" % s)
|
2013-05-08 00:23:51 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
else:
|
|
|
|
offset = ifd.save(fp)
|
|
|
|
|
|
|
|
ImageFile._save(im, fp, [
|
2014-07-28 20:00:06 +04:00
|
|
|
("raw", (0, 0)+im.size, offset, (rawmode, stride, 1))
|
2013-03-09 07:51:59 +04:00
|
|
|
])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# -- helper for multi-page save --
|
2012-10-16 01:18:27 +04:00
|
|
|
if "_debug_multipage" in im.encoderinfo:
|
2014-07-28 20:00:06 +04:00
|
|
|
# just to access o32 and o16 (using correct byte order)
|
2010-07-31 06:52:47 +04:00
|
|
|
im._debug_multipage = ifd
|
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Register
|
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(TiffImageFile.format, TiffImageFile, _accept)
|
|
|
|
Image.register_save(TiffImageFile.format, _save)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_extension(TiffImageFile.format, ".tif")
|
|
|
|
Image.register_extension(TiffImageFile.format, ".tiff")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_mime(TiffImageFile.format, "image/tiff")
|