2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# PNG support code
|
|
|
|
#
|
|
|
|
# See "PNG (Portable Network Graphics) Specification, version 1.0;
|
|
|
|
# W3C Recommendation", 1996-10-01, Thomas Boutell (ed.).
|
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 1996-05-06 fl Created (couldn't resist it)
|
|
|
|
# 1996-12-14 fl Upgraded, added read and verify support (0.2)
|
|
|
|
# 1996-12-15 fl Separate PNG stream parser
|
|
|
|
# 1996-12-29 fl Added write support, added getchunks
|
|
|
|
# 1996-12-30 fl Eliminated circular references in decoder (0.3)
|
|
|
|
# 1998-07-12 fl Read/write 16-bit images as mode I (0.4)
|
|
|
|
# 2001-02-08 fl Added transparency support (from Zircon) (0.5)
|
|
|
|
# 2001-04-16 fl Don't close data source in "open" method (0.6)
|
|
|
|
# 2004-02-24 fl Don't even pretend to support interlaced files (0.7)
|
|
|
|
# 2004-08-31 fl Do basic sanity check on chunk identifiers (0.8)
|
|
|
|
# 2004-09-20 fl Added PngInfo chunk container
|
|
|
|
# 2004-12-18 fl Added DPI read support (based on code by Niki Spahiev)
|
|
|
|
# 2008-08-13 fl Added tRNS support for RGB images
|
|
|
|
# 2009-03-06 fl Support for preserving ICC profiles (by Florian Hoech)
|
|
|
|
# 2009-03-08 fl Added zTXT support (from Lowell Alleman)
|
|
|
|
# 2009-03-29 fl Read interlaced PNG files (from Conrado Porto Lopes Gouvua)
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2009 by Secret Labs AB
|
|
|
|
# Copyright (c) 1996 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2012-10-16 06:27:35 +04:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
import logging
|
2012-10-11 02:11:13 +04:00
|
|
|
import re
|
2015-02-06 21:58:07 +03:00
|
|
|
import zlib
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import Image, ImageFile, ImagePalette, _binary
|
2015-02-06 21:58:07 +03:00
|
|
|
|
2015-08-25 15:27:18 +03:00
|
|
|
__version__ = "0.9"
|
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
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
|
|
|
|
i16 = _binary.i16be
|
|
|
|
i32 = _binary.i32be
|
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
|
|
|
is_cid = re.compile(b"\w\w\w\w").match
|
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
|
|
|
_MAGIC = b"\211PNG\r\n\032\n"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
_MODES = {
|
|
|
|
# supported bits/color combinations, and corresponding modes/rawmodes
|
2014-08-26 17:47:10 +04:00
|
|
|
(1, 0): ("1", "1"),
|
|
|
|
(2, 0): ("L", "L;2"),
|
|
|
|
(4, 0): ("L", "L;4"),
|
|
|
|
(8, 0): ("L", "L"),
|
|
|
|
(16, 0): ("I", "I;16B"),
|
|
|
|
(8, 2): ("RGB", "RGB"),
|
|
|
|
(16, 2): ("RGB", "RGB;16B"),
|
|
|
|
(1, 3): ("P", "P;1"),
|
|
|
|
(2, 3): ("P", "P;2"),
|
|
|
|
(4, 3): ("P", "P;4"),
|
|
|
|
(8, 3): ("P", "P"),
|
|
|
|
(8, 4): ("LA", "LA"),
|
|
|
|
(16, 4): ("RGBA", "LA;16B"), # LA;16B->LA not yet available
|
|
|
|
(8, 6): ("RGBA", "RGBA"),
|
|
|
|
(16, 6): ("RGBA", "RGBA;16B"),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-22 00:00:25 +04:00
|
|
|
_simple_palette = re.compile(b'^\xff+\x00\xff*$')
|
2015-05-23 01:37:50 +03:00
|
|
|
_null_palette = re.compile(b'^\x00*$')
|
2013-03-11 23:33:04 +04:00
|
|
|
|
2014-12-30 04:10:27 +03:00
|
|
|
# Maximum decompressed size for a iTXt or zTXt chunk.
|
|
|
|
# Eliminates decompression bombs where compressed chunks can expand 1000x
|
|
|
|
MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
|
|
|
|
# Set the maximum total text chunk size.
|
|
|
|
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2014-12-01 10:31:29 +03:00
|
|
|
def _safe_zlib_decompress(s):
|
|
|
|
dobj = zlib.decompressobj()
|
2014-12-30 04:10:27 +03:00
|
|
|
plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)
|
2014-12-01 10:31:29 +03:00
|
|
|
if dobj.unconsumed_tail:
|
|
|
|
raise ValueError("Decompressed Data Too Large")
|
|
|
|
return plaintext
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Support classes. Suitable for PNG and related formats like MNG etc.
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class ChunkStream(object):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
|
|
|
self.fp = fp
|
|
|
|
self.queue = []
|
|
|
|
|
|
|
|
if not hasattr(Image.core, "crc32"):
|
|
|
|
self.crc = self.crc_skip
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
"Fetch a new chunk. Returns header information."
|
|
|
|
|
|
|
|
if self.queue:
|
2014-05-21 21:45:02 +04:00
|
|
|
cid, pos, length = self.queue[-1]
|
2010-07-31 06:52:47 +04:00
|
|
|
del self.queue[-1]
|
|
|
|
self.fp.seek(pos)
|
|
|
|
else:
|
|
|
|
s = self.fp.read(8)
|
|
|
|
cid = s[4:]
|
|
|
|
pos = self.fp.tell()
|
2014-05-21 21:45:02 +04:00
|
|
|
length = i32(s)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if not is_cid(cid):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("broken PNG file (chunk %s)" % repr(cid))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
return cid, pos, length
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.queue = self.crc = self.fp = None
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def push(self, cid, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
self.queue.append((cid, pos, length))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def call(self, cid, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
"Call the appropriate chunk handler"
|
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("STREAM %s %s %s", cid, pos, length)
|
2014-05-21 21:45:02 +04:00
|
|
|
return getattr(self, "chunk_" + cid.decode('ascii'))(pos, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def crc(self, cid, data):
|
|
|
|
"Read and verify checksum"
|
|
|
|
|
|
|
|
crc1 = Image.core.crc32(data, Image.core.crc32(cid))
|
|
|
|
crc2 = i16(self.fp.read(2)), i16(self.fp.read(2))
|
|
|
|
if crc1 != crc2:
|
2014-08-26 17:47:10 +04:00
|
|
|
raise SyntaxError("broken PNG file"
|
|
|
|
"(bad header checksum in %s)" % cid)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def crc_skip(self, cid, data):
|
|
|
|
"Read checksum. Used if the C module is not present"
|
|
|
|
|
|
|
|
self.fp.read(4)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
def verify(self, endchunk=b"IEND"):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# Simple approach; just calculate checksum for all remaining
|
|
|
|
# blocks. Must be called directly after open.
|
|
|
|
|
|
|
|
cids = []
|
|
|
|
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2014-05-21 21:45:02 +04:00
|
|
|
cid, pos, length = self.read()
|
2010-07-31 06:52:47 +04:00
|
|
|
if cid == endchunk:
|
|
|
|
break
|
2014-05-21 21:45:02 +04:00
|
|
|
self.crc(cid, ImageFile._safe_read(self.fp, length))
|
2010-07-31 06:52:47 +04:00
|
|
|
cids.append(cid)
|
|
|
|
|
|
|
|
return cids
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
class iTXt(str):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""
|
|
|
|
Subclass of string to allow iTXt chunks to look like strings while
|
|
|
|
keeping their extra information
|
|
|
|
|
|
|
|
"""
|
2014-07-23 11:24:18 +04:00
|
|
|
@staticmethod
|
|
|
|
def __new__(cls, text, lang, tkey):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""
|
|
|
|
:param value: value for this key
|
|
|
|
:param lang: language code
|
|
|
|
:param tkey: UTF-8 version of the key name
|
|
|
|
"""
|
2015-03-05 22:07:16 +03:00
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
self = str.__new__(cls, text)
|
|
|
|
self.lang = lang
|
|
|
|
self.tkey = tkey
|
|
|
|
return self
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class PngInfo(object):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""
|
|
|
|
PNG chunk container (for use with save(pnginfo=))
|
|
|
|
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.chunks = []
|
|
|
|
|
|
|
|
def add(self, cid, data):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""Appends an arbitrary chunk. Use with caution.
|
|
|
|
|
|
|
|
:param cid: a byte string, 4 bytes long.
|
|
|
|
:param data: a byte string of the encoded data
|
|
|
|
|
|
|
|
"""
|
2015-03-05 22:07:16 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
self.chunks.append((cid, data))
|
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
def add_itxt(self, key, value, lang="", tkey="", zip=False):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""Appends an iTXt chunk.
|
|
|
|
|
|
|
|
:param key: latin-1 encodable text key name
|
|
|
|
:param value: value for this key
|
|
|
|
:param lang: language code
|
|
|
|
:param tkey: UTF-8 version of the key name
|
|
|
|
:param zip: compression flag
|
|
|
|
|
|
|
|
"""
|
2015-03-05 22:07:16 +03:00
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
if not isinstance(key, bytes):
|
|
|
|
key = key.encode("latin-1", "strict")
|
|
|
|
if not isinstance(value, bytes):
|
|
|
|
value = value.encode("utf-8", "strict")
|
|
|
|
if not isinstance(lang, bytes):
|
|
|
|
lang = lang.encode("utf-8", "strict")
|
|
|
|
if not isinstance(tkey, bytes):
|
|
|
|
tkey = tkey.encode("utf-8", "strict")
|
|
|
|
|
|
|
|
if zip:
|
2014-08-26 17:47:10 +04:00
|
|
|
self.add(b"iTXt", key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" +
|
|
|
|
zlib.compress(value))
|
2014-07-23 11:24:18 +04:00
|
|
|
else:
|
2014-08-26 17:47:10 +04:00
|
|
|
self.add(b"iTXt", key + b"\0\0\0" + lang + b"\0" + tkey + b"\0" +
|
|
|
|
value)
|
2014-07-23 11:24:18 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def add_text(self, key, value, zip=0):
|
2014-11-20 02:35:01 +03:00
|
|
|
"""Appends a text chunk.
|
|
|
|
|
|
|
|
:param key: latin-1 encodable text key name
|
|
|
|
:param value: value for this key, text or an
|
|
|
|
:py:class:`PIL.PngImagePlugin.iTXt` instance
|
|
|
|
:param zip: compression flag
|
2015-03-05 22:07:16 +03:00
|
|
|
|
2014-11-20 02:35:01 +03:00
|
|
|
"""
|
2014-07-23 11:24:18 +04:00
|
|
|
if isinstance(value, iTXt):
|
|
|
|
return self.add_itxt(key, value, value.lang, value.tkey, bool(zip))
|
|
|
|
|
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
|
|
|
# The tEXt chunk stores latin-1 text
|
2014-07-23 18:43:52 +04:00
|
|
|
if not isinstance(value, bytes):
|
|
|
|
try:
|
|
|
|
value = value.encode('latin-1', 'strict')
|
|
|
|
except UnicodeError:
|
|
|
|
return self.add_itxt(key, value, zip=bool(zip))
|
|
|
|
|
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 not isinstance(key, bytes):
|
|
|
|
key = key.encode('latin-1', 'strict')
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if zip:
|
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.add(b"zTXt", key + b"\0\0" + zlib.compress(value))
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
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.add(b"tEXt", key + b"\0" + value)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# PNG image stream (IHDR/IEND)
|
|
|
|
|
|
|
|
class PngStream(ChunkStream):
|
|
|
|
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
|
|
|
ChunkStream.__init__(self, fp)
|
|
|
|
|
|
|
|
# local copies of Image attributes
|
|
|
|
self.im_info = {}
|
|
|
|
self.im_text = {}
|
2014-08-26 17:47:10 +04:00
|
|
|
self.im_size = (0, 0)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_mode = None
|
|
|
|
self.im_tile = None
|
|
|
|
self.im_palette = None
|
|
|
|
|
2014-12-30 04:10:27 +03:00
|
|
|
self.text_memory = 0
|
|
|
|
|
|
|
|
def check_text_memory(self, chunklen):
|
|
|
|
self.text_memory += chunklen
|
|
|
|
if self.text_memory > MAX_TEXT_MEMORY:
|
|
|
|
raise ValueError("Too much memory used in text chunks: %s>MAX_TEXT_MEMORY" %
|
|
|
|
self.text_memory)
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_iCCP(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# ICC profile
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
# according to PNG spec, the iCCP chunk contains:
|
|
|
|
# Profile name 1-79 bytes (character string)
|
|
|
|
# Null separator 1 byte (null character)
|
|
|
|
# Compression method 1 byte (0)
|
|
|
|
# Compressed profile n bytes (zlib with deflate compression)
|
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
|
|
|
i = s.find(b"\0")
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("iCCP profile name %s", s[:i])
|
|
|
|
logger.debug("Compression method %s", i8(s[i]))
|
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
|
|
|
comp_method = i8(s[i])
|
2010-07-31 06:52:47 +04:00
|
|
|
if comp_method != 0:
|
2014-08-26 17:47:10 +04:00
|
|
|
raise SyntaxError("Unknown compression method %s in iCCP chunk" %
|
|
|
|
comp_method)
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
2014-12-01 10:31:29 +03:00
|
|
|
icc_profile = _safe_zlib_decompress(s[i+2:])
|
2010-07-31 06:52:47 +04:00
|
|
|
except zlib.error:
|
2014-08-26 17:47:10 +04:00
|
|
|
icc_profile = None # FIXME
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_info["icc_profile"] = icc_profile
|
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_IHDR(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image header
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_size = i32(s), i32(s[4:])
|
|
|
|
try:
|
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.im_mode, self.im_rawmode = _MODES[(i8(s[8]), i8(s[9]))]
|
2010-07-31 06:52:47 +04:00
|
|
|
except:
|
|
|
|
pass
|
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 i8(s[12]):
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_info["interlace"] = 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
|
|
|
if i8(s[11]):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("unknown filter category")
|
2010-07-31 06:52:47 +04:00
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_IDAT(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image data
|
2014-08-26 17:47:10 +04:00
|
|
|
self.im_tile = [("zip", (0, 0)+self.im_size, pos, self.im_rawmode)]
|
2014-05-21 21:45:02 +04:00
|
|
|
self.im_idat = length
|
2010-07-31 06:52:47 +04:00
|
|
|
raise EOFError
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_IEND(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# end of PNG image
|
|
|
|
raise EOFError
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_PLTE(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# palette
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
if self.im_mode == "P":
|
|
|
|
self.im_palette = "RGB", s
|
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_tRNS(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# transparency
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
if self.im_mode == "P":
|
2013-03-11 23:33:04 +04:00
|
|
|
if _simple_palette.match(s):
|
|
|
|
i = s.find(b"\0")
|
|
|
|
if i >= 0:
|
|
|
|
self.im_info["transparency"] = i
|
2015-05-23 01:37:50 +03:00
|
|
|
elif _null_palette.match(s):
|
|
|
|
self.im_info["transparency"] = 0
|
2013-03-11 23:33:04 +04:00
|
|
|
else:
|
|
|
|
self.im_info["transparency"] = s
|
2010-07-31 06:52:47 +04:00
|
|
|
elif self.im_mode == "L":
|
|
|
|
self.im_info["transparency"] = i16(s)
|
|
|
|
elif self.im_mode == "RGB":
|
|
|
|
self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:])
|
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_gAMA(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# gamma setting
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_info["gamma"] = i32(s) / 100000.0
|
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_pHYs(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# pixels per unit
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
px, py = i32(s), i32(s[4:])
|
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
|
|
|
unit = i8(s[8])
|
2014-08-26 17:47:10 +04:00
|
|
|
if unit == 1: # meter
|
2010-07-31 06:52:47 +04:00
|
|
|
dpi = int(px * 0.0254 + 0.5), int(py * 0.0254 + 0.5)
|
|
|
|
self.im_info["dpi"] = dpi
|
|
|
|
elif unit == 0:
|
|
|
|
self.im_info["aspect"] = px, py
|
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_tEXt(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# text
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
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
|
|
|
k, v = s.split(b"\0", 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
except ValueError:
|
2014-08-26 17:47:10 +04:00
|
|
|
# fallback for broken tEXt tags
|
|
|
|
k = s
|
|
|
|
v = b""
|
2010-07-31 06:52:47 +04:00
|
|
|
if k:
|
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 not str:
|
|
|
|
k = k.decode('latin-1', 'strict')
|
|
|
|
v = v.decode('latin-1', 'replace')
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im_info[k] = self.im_text[k] = v
|
2014-12-30 04:10:27 +03:00
|
|
|
self.check_text_memory(len(v))
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
return s
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
def chunk_zTXt(self, pos, length):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# compressed text
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2013-08-20 16:17:17 +04:00
|
|
|
try:
|
|
|
|
k, v = s.split(b"\0", 1)
|
|
|
|
except ValueError:
|
2014-08-26 17:47:10 +04:00
|
|
|
k = s
|
|
|
|
v = b""
|
2013-08-20 16:17:17 +04:00
|
|
|
if v:
|
|
|
|
comp_method = i8(v[0])
|
|
|
|
else:
|
|
|
|
comp_method = 0
|
2010-07-31 06:52:47 +04:00
|
|
|
if comp_method != 0:
|
2014-08-26 17:47:10 +04:00
|
|
|
raise SyntaxError("Unknown compression method %s in zTXt chunk" %
|
|
|
|
comp_method)
|
2013-08-20 16:17:17 +04:00
|
|
|
try:
|
2014-12-01 10:31:29 +03:00
|
|
|
v = _safe_zlib_decompress(v[1:])
|
2013-08-20 16:17:17 +04:00
|
|
|
except zlib.error:
|
|
|
|
v = b""
|
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
|
|
|
|
2013-08-20 16:17:17 +04:00
|
|
|
if k:
|
|
|
|
if bytes is not str:
|
|
|
|
k = k.decode('latin-1', 'strict')
|
|
|
|
v = v.decode('latin-1', 'replace')
|
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
|
|
|
|
2013-08-20 16:17:17 +04:00
|
|
|
self.im_info[k] = self.im_text[k] = v
|
2014-12-30 04:10:27 +03:00
|
|
|
self.check_text_memory(len(v))
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
return s
|
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
def chunk_iTXt(self, pos, length):
|
|
|
|
|
|
|
|
# international text
|
|
|
|
r = s = ImageFile._safe_read(self.fp, length)
|
|
|
|
try:
|
|
|
|
k, r = r.split(b"\0", 1)
|
|
|
|
except ValueError:
|
|
|
|
return s
|
|
|
|
if len(r) < 2:
|
|
|
|
return s
|
|
|
|
cf, cm, r = i8(r[0]), i8(r[1]), r[2:]
|
|
|
|
try:
|
|
|
|
lang, tk, v = r.split(b"\0", 2)
|
|
|
|
except ValueError:
|
|
|
|
return s
|
|
|
|
if cf != 0:
|
|
|
|
if cm == 0:
|
|
|
|
try:
|
2014-12-01 10:31:29 +03:00
|
|
|
v = _safe_zlib_decompress(v)
|
2014-07-23 11:24:18 +04:00
|
|
|
except zlib.error:
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
if bytes is not str:
|
|
|
|
try:
|
|
|
|
k = k.decode("latin-1", "strict")
|
|
|
|
lang = lang.decode("utf-8", "strict")
|
|
|
|
tk = tk.decode("utf-8", "strict")
|
|
|
|
v = v.decode("utf-8", "strict")
|
|
|
|
except UnicodeError:
|
|
|
|
return s
|
|
|
|
|
|
|
|
self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)
|
2014-12-30 04:10:27 +03:00
|
|
|
self.check_text_memory(len(v))
|
2015-03-05 22:07:16 +03:00
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
return s
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# PNG reader
|
|
|
|
|
|
|
|
def _accept(prefix):
|
|
|
|
return prefix[:8] == _MAGIC
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for PNG images.
|
|
|
|
|
|
|
|
class PngImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "PNG"
|
|
|
|
format_description = "Portable network graphics"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
if self.fp.read(8) != _MAGIC:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a PNG file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Parse headers up to the first IDAT chunk
|
|
|
|
|
|
|
|
self.png = PngStream(self.fp)
|
|
|
|
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# get next chunk
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
cid, pos, length = self.png.read()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
try:
|
2014-05-21 21:45:02 +04:00
|
|
|
s = self.png.call(cid, pos, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
except EOFError:
|
|
|
|
break
|
|
|
|
except AttributeError:
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("%s %s %s (unknown)", cid, pos, length)
|
2014-05-21 21:45:02 +04:00
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.png.crc(cid, s)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Copy relevant attributes from the PngStream. An alternative
|
|
|
|
# would be to let the PngStream class modify these attributes
|
|
|
|
# directly, but that introduces circular references which are
|
|
|
|
# difficult to break if things go wrong in the decoder...
|
|
|
|
# (believe me, I've tried ;-)
|
|
|
|
|
|
|
|
self.mode = self.png.im_mode
|
|
|
|
self.size = self.png.im_size
|
|
|
|
self.info = self.png.im_info
|
2014-08-26 17:47:10 +04:00
|
|
|
self.text = self.png.im_text # experimental
|
2010-07-31 06:52:47 +04:00
|
|
|
self.tile = self.png.im_tile
|
|
|
|
|
|
|
|
if self.png.im_palette:
|
|
|
|
rawmode, data = self.png.im_palette
|
|
|
|
self.palette = ImagePalette.raw(rawmode, data)
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
self.__idat = length # used by load_read()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def verify(self):
|
|
|
|
"Verify PNG file"
|
|
|
|
|
|
|
|
if self.fp is None:
|
|
|
|
raise RuntimeError("verify must be called directly after open")
|
|
|
|
|
|
|
|
# back up to beginning of IDAT block
|
|
|
|
self.fp.seek(self.tile[0][2] - 8)
|
|
|
|
|
|
|
|
self.png.verify()
|
|
|
|
self.png.close()
|
|
|
|
|
|
|
|
self.fp = None
|
|
|
|
|
|
|
|
def load_prepare(self):
|
|
|
|
"internal: prepare to read PNG file"
|
|
|
|
|
|
|
|
if self.info.get("interlace"):
|
|
|
|
self.decoderconfig = self.decoderconfig + (1,)
|
|
|
|
|
|
|
|
ImageFile.ImageFile.load_prepare(self)
|
|
|
|
|
2014-05-21 22:33:49 +04:00
|
|
|
def load_read(self, read_bytes):
|
2010-07-31 06:52:47 +04:00
|
|
|
"internal: read more image data"
|
|
|
|
|
|
|
|
while self.__idat == 0:
|
|
|
|
# end of chunk, skip forward to next one
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
self.fp.read(4) # CRC
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
cid, pos, length = self.png.read()
|
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
|
|
|
if cid not in [b"IDAT", b"DDAT"]:
|
2014-05-21 21:45:02 +04:00
|
|
|
self.png.push(cid, pos, length)
|
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
|
|
|
return b""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
self.__idat = length # empty chunks are allowed
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# read more data from this chunk
|
2014-05-21 22:33:49 +04:00
|
|
|
if read_bytes <= 0:
|
|
|
|
read_bytes = self.__idat
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2014-05-21 22:33:49 +04:00
|
|
|
read_bytes = min(read_bytes, self.__idat)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 22:33:49 +04:00
|
|
|
self.__idat = self.__idat - read_bytes
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-05-21 22:33:49 +04:00
|
|
|
return self.fp.read(read_bytes)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def load_end(self):
|
|
|
|
"internal: finished reading image data"
|
|
|
|
|
|
|
|
self.png.close()
|
|
|
|
self.png = None
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# PNG writer
|
|
|
|
|
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
|
|
|
o8 = _binary.o8
|
|
|
|
o16 = _binary.o16be
|
|
|
|
o32 = _binary.o32be
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
_OUTMODES = {
|
|
|
|
# supported PIL modes, and corresponding rawmodes/bits/color combinations
|
2014-08-26 17:47:10 +04:00
|
|
|
"1": ("1", b'\x01\x00'),
|
|
|
|
"L;1": ("L;1", b'\x01\x00'),
|
|
|
|
"L;2": ("L;2", b'\x02\x00'),
|
|
|
|
"L;4": ("L;4", b'\x04\x00'),
|
|
|
|
"L": ("L", b'\x08\x00'),
|
|
|
|
"LA": ("LA", b'\x08\x04'),
|
|
|
|
"I": ("I;16B", b'\x10\x00'),
|
|
|
|
"P;1": ("P;1", b'\x01\x03'),
|
|
|
|
"P;2": ("P;2", b'\x02\x03'),
|
|
|
|
"P;4": ("P;4", b'\x04\x03'),
|
|
|
|
"P": ("P", b'\x08\x03'),
|
|
|
|
"RGB": ("RGB", b'\x08\x02'),
|
|
|
|
"RGBA": ("RGBA", b'\x08\x06'),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def putchunk(fp, cid, *data):
|
|
|
|
"Write a PNG chunk (including CRC field)"
|
|
|
|
|
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
|
|
|
data = b"".join(data)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
fp.write(o32(len(data)) + cid)
|
|
|
|
fp.write(data)
|
|
|
|
hi, lo = Image.core.crc32(data, Image.core.crc32(cid))
|
|
|
|
fp.write(o16(hi) + o16(lo))
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class _idat(object):
|
2010-07-31 06:52:47 +04:00
|
|
|
# wrap output from the encoder in IDAT chunks
|
|
|
|
|
|
|
|
def __init__(self, fp, chunk):
|
|
|
|
self.fp = fp
|
|
|
|
self.chunk = chunk
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def write(self, data):
|
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.chunk(self.fp, b"IDAT", data)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _save(im, fp, filename, chunk=putchunk, check=0):
|
|
|
|
# save an image to disk (called by the save method)
|
|
|
|
|
|
|
|
mode = im.mode
|
|
|
|
|
|
|
|
if mode == "P":
|
|
|
|
|
|
|
|
#
|
|
|
|
# attempt to minimize storage requirements for palette images
|
2012-10-16 01:18:27 +04:00
|
|
|
if "bits" in im.encoderinfo:
|
2010-07-31 06:52:47 +04:00
|
|
|
# number of bits specified by user
|
2013-08-02 18:32:49 +04:00
|
|
|
colors = 1 << im.encoderinfo["bits"]
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
|
|
|
# check palette contents
|
2013-08-02 18:32:49 +04:00
|
|
|
if im.palette:
|
2014-02-05 16:49:08 +04:00
|
|
|
colors = max(min(len(im.palette.getdata()[1])//3, 256), 2)
|
2013-08-02 18:32:49 +04:00
|
|
|
else:
|
|
|
|
colors = 256
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-08-02 18:32:49 +04:00
|
|
|
if colors <= 2:
|
2010-07-31 06:52:47 +04:00
|
|
|
bits = 1
|
2013-08-02 18:32:49 +04:00
|
|
|
elif colors <= 4:
|
2010-07-31 06:52:47 +04:00
|
|
|
bits = 2
|
2013-08-02 18:32:49 +04:00
|
|
|
elif colors <= 16:
|
2010-07-31 06:52:47 +04:00
|
|
|
bits = 4
|
|
|
|
else:
|
|
|
|
bits = 8
|
|
|
|
if bits != 8:
|
|
|
|
mode = "%s;%d" % (mode, bits)
|
|
|
|
|
|
|
|
# encoder options
|
2012-10-16 01:18:27 +04:00
|
|
|
if "dictionary" in im.encoderinfo:
|
2010-07-31 06:52:47 +04:00
|
|
|
dictionary = im.encoderinfo["dictionary"]
|
|
|
|
else:
|
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
|
|
|
dictionary = b""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-11 23:33:04 +04:00
|
|
|
im.encoderconfig = ("optimize" in im.encoderinfo,
|
2014-08-26 17:47:10 +04:00
|
|
|
im.encoderinfo.get("compress_level", -1),
|
|
|
|
im.encoderinfo.get("compress_type", -1),
|
|
|
|
dictionary)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# get the corresponding PNG mode
|
|
|
|
try:
|
|
|
|
rawmode, mode = _OUTMODES[mode]
|
|
|
|
except KeyError:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise IOError("cannot write mode %s as PNG" % mode)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if check:
|
|
|
|
return check
|
|
|
|
|
|
|
|
#
|
|
|
|
# write minimal PNG file
|
|
|
|
|
|
|
|
fp.write(_MAGIC)
|
|
|
|
|
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
|
|
|
chunk(fp, b"IHDR",
|
2014-08-26 17:47:10 +04:00
|
|
|
o32(im.size[0]), o32(im.size[1]), # 0: size
|
|
|
|
mode, # 8: depth/type
|
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
|
|
|
b'\0', # 10: compression
|
|
|
|
b'\0', # 11: filter category
|
|
|
|
b'\0') # 12: interlace flag
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if im.mode == "P":
|
2013-08-02 18:32:49 +04:00
|
|
|
palette_byte_number = (2 ** bits) * 3
|
|
|
|
palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
|
|
|
|
while len(palette_bytes) < palette_byte_number:
|
|
|
|
palette_bytes += b'\0'
|
|
|
|
chunk(fp, b"PLTE", palette_bytes)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
transparency = im.encoderinfo.get('transparency',
|
|
|
|
im.info.get('transparency', None))
|
2014-05-21 21:45:02 +04:00
|
|
|
|
2014-02-25 12:50:42 +04:00
|
|
|
if transparency or transparency == 0:
|
2010-07-31 06:52:47 +04:00
|
|
|
if im.mode == "P":
|
2013-03-21 21:47:37 +04:00
|
|
|
# limit to actual palette size
|
2013-03-21 21:16:00 +04:00
|
|
|
alpha_bytes = 2**bits
|
2013-11-27 02:59:03 +04:00
|
|
|
if isinstance(transparency, bytes):
|
|
|
|
chunk(fp, b"tRNS", transparency[:alpha_bytes])
|
2013-03-21 19:54:04 +04:00
|
|
|
else:
|
2013-11-27 02:59:03 +04:00
|
|
|
transparency = max(0, min(255, transparency))
|
2013-03-21 21:43:22 +04:00
|
|
|
alpha = b'\xFF' * transparency + b'\0'
|
|
|
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "L":
|
2013-11-27 02:59:03 +04:00
|
|
|
transparency = max(0, min(65535, transparency))
|
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
|
|
|
chunk(fp, b"tRNS", o16(transparency))
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "RGB":
|
2013-11-27 02:59:03 +04:00
|
|
|
red, green, blue = transparency
|
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
|
|
|
chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue))
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2013-11-27 02:59:03 +04:00
|
|
|
if "transparency" in im.encoderinfo:
|
|
|
|
# don't bother with transparency if it's an RGBA
|
2014-05-21 21:45:02 +04:00
|
|
|
# and it's in the info dict. It's probably just stale.
|
2013-11-27 02:59:03 +04:00
|
|
|
raise IOError("cannot use transparency for this mode")
|
2013-03-11 23:33:04 +04:00
|
|
|
else:
|
|
|
|
if im.mode == "P" and im.im.getpalettemode() == "RGBA":
|
|
|
|
alpha = im.im.getpalette("RGBA", "A")
|
2013-03-11 23:58:54 +04:00
|
|
|
alpha_bytes = 2**bits
|
|
|
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
dpi = im.encoderinfo.get("dpi")
|
|
|
|
if dpi:
|
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
|
|
|
chunk(fp, b"pHYs",
|
2010-07-31 06:52:47 +04:00
|
|
|
o32(int(dpi[0] / 0.0254 + 0.5)),
|
|
|
|
o32(int(dpi[1] / 0.0254 + 0.5)),
|
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
|
|
|
b'\x01')
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
info = im.encoderinfo.get("pnginfo")
|
|
|
|
if info:
|
|
|
|
for cid, data in info.chunks:
|
|
|
|
chunk(fp, cid, data)
|
|
|
|
|
|
|
|
# ICC profile writing support -- 2008-06-06 Florian Hoech
|
2014-01-19 22:09:40 +04:00
|
|
|
if im.info.get("icc_profile"):
|
2010-07-31 06:52:47 +04:00
|
|
|
# ICC profile
|
|
|
|
# according to PNG spec, the iCCP chunk contains:
|
|
|
|
# Profile name 1-79 bytes (character string)
|
|
|
|
# Null separator 1 byte (null character)
|
|
|
|
# Compression method 1 byte (0)
|
|
|
|
# Compressed profile n bytes (zlib with deflate compression)
|
2014-01-19 22:09:40 +04:00
|
|
|
name = b"ICC Profile"
|
|
|
|
data = name + b"\0\0" + zlib.compress(im.info["icc_profile"])
|
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
|
|
|
chunk(fp, b"iCCP", data)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
ImageFile._save(im, _idat(fp, chunk),
|
|
|
|
[("zip", (0, 0)+im.size, 0, rawmode)])
|
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
|
|
|
chunk(fp, b"IEND", b"")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-02 16:48:22 +03:00
|
|
|
if hasattr(fp, "flush"):
|
2010-07-31 06:52:47 +04:00
|
|
|
fp.flush()
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# PNG chunk converter
|
|
|
|
|
|
|
|
def getchunks(im, **params):
|
|
|
|
"""Return a list of PNG chunks representing this image."""
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class collector(object):
|
2010-07-31 06:52:47 +04:00
|
|
|
data = []
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def write(self, data):
|
|
|
|
pass
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def append(self, chunk):
|
|
|
|
self.data.append(chunk)
|
|
|
|
|
|
|
|
def append(fp, cid, *data):
|
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
|
|
|
data = b"".join(data)
|
2010-07-31 06:52:47 +04:00
|
|
|
hi, lo = Image.core.crc32(data, Image.core.crc32(cid))
|
|
|
|
crc = o16(hi) + o16(lo)
|
|
|
|
fp.append((cid, data, crc))
|
|
|
|
|
|
|
|
fp = collector()
|
|
|
|
|
|
|
|
try:
|
|
|
|
im.encoderinfo = params
|
|
|
|
_save(im, fp, None, append)
|
|
|
|
finally:
|
|
|
|
del im.encoderinfo
|
|
|
|
|
|
|
|
return fp.data
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(PngImageFile.format, PngImageFile, _accept)
|
|
|
|
Image.register_save(PngImageFile.format, _save)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_extension(PngImageFile.format, ".png")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_mime(PngImageFile.format, "image/png")
|