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.
|
|
|
|
|
#
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
import itertools
|
2015-02-06 21:58:07 +03:00
|
|
|
|
import logging
|
2012-10-11 02:11:13 +04:00
|
|
|
|
import re
|
2016-04-04 13:08:22 +03:00
|
|
|
|
import struct
|
2019-12-01 06:50:21 +03:00
|
|
|
|
import warnings
|
2019-07-06 23:40:53 +03:00
|
|
|
|
import zlib
|
2022-01-15 01:02:31 +03:00
|
|
|
|
from enum import IntEnum
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
|
2020-09-01 20:16:46 +03:00
|
|
|
|
from ._binary import i16be as i16
|
|
|
|
|
from ._binary import i32be as i32
|
|
|
|
|
from ._binary import o8
|
|
|
|
|
from ._binary import o16be as o16
|
|
|
|
|
from ._binary import o32be as o32
|
2022-04-05 16:33:20 +03:00
|
|
|
|
from ._deprecate import deprecate
|
2015-02-06 21:58:07 +03:00
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2022-03-04 08:42:24 +03:00
|
|
|
|
is_cid = re.compile(rb"\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
|
2019-03-26 23:41:33 +03:00
|
|
|
|
# Greyscale
|
2019-03-21 16:28:20 +03:00
|
|
|
|
(1, 0): ("1", "1"),
|
|
|
|
|
(2, 0): ("L", "L;2"),
|
|
|
|
|
(4, 0): ("L", "L;4"),
|
|
|
|
|
(8, 0): ("L", "L"),
|
2014-08-26 17:47:10 +04:00
|
|
|
|
(16, 0): ("I", "I;16B"),
|
2019-03-26 23:41:33 +03:00
|
|
|
|
# Truecolour
|
2019-03-21 16:28:20 +03:00
|
|
|
|
(8, 2): ("RGB", "RGB"),
|
2014-08-26 17:47:10 +04:00
|
|
|
|
(16, 2): ("RGB", "RGB;16B"),
|
2019-03-26 23:41:33 +03:00
|
|
|
|
# Indexed-colour
|
2019-03-21 16:28:20 +03:00
|
|
|
|
(1, 3): ("P", "P;1"),
|
|
|
|
|
(2, 3): ("P", "P;2"),
|
|
|
|
|
(4, 3): ("P", "P;4"),
|
|
|
|
|
(8, 3): ("P", "P"),
|
2019-03-26 23:41:33 +03:00
|
|
|
|
# Greyscale with alpha
|
2019-03-21 16:28:20 +03:00
|
|
|
|
(8, 4): ("LA", "LA"),
|
2014-08-26 17:47:10 +04:00
|
|
|
|
(16, 4): ("RGBA", "LA;16B"), # LA;16B->LA not yet available
|
2019-03-26 23:41:33 +03:00
|
|
|
|
# Truecolour with alpha
|
2019-03-21 16:28:20 +03:00
|
|
|
|
(8, 6): ("RGBA", "RGBA"),
|
2014-08-26 17:47:10 +04:00
|
|
|
|
(16, 6): ("RGBA", "RGBA;16B"),
|
2010-07-31 06:52:47 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
_simple_palette = re.compile(b"^\xff*\x00\xff*$")
|
2013-03-11 23:33:04 +04:00
|
|
|
|
|
2014-12-30 04:10:27 +03:00
|
|
|
|
MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
|
2020-07-09 23:16:25 +03:00
|
|
|
|
"""
|
|
|
|
|
Maximum decompressed size for a iTXt or zTXt chunk.
|
2020-07-10 16:17:10 +03:00
|
|
|
|
Eliminates decompression bombs where compressed chunks can expand 1000x.
|
2020-07-09 23:16:25 +03:00
|
|
|
|
See :ref:`Text in PNG File Format<png-text>`.
|
|
|
|
|
"""
|
2014-12-30 04:10:27 +03:00
|
|
|
|
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
|
2020-07-09 23:16:25 +03:00
|
|
|
|
"""
|
|
|
|
|
Set the maximum total text chunk size.
|
|
|
|
|
See :ref:`Text in PNG File Format<png-text>`.
|
|
|
|
|
"""
|
2014-12-30 04:10:27 +03:00
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
# APNG frame disposal modes
|
2022-01-15 01:02:31 +03:00
|
|
|
|
class Disposal(IntEnum):
|
|
|
|
|
OP_NONE = 0
|
|
|
|
|
"""
|
|
|
|
|
No disposal is done on this frame before rendering the next frame.
|
|
|
|
|
See :ref:`Saving APNG sequences<apng-saving>`.
|
|
|
|
|
"""
|
|
|
|
|
OP_BACKGROUND = 1
|
|
|
|
|
"""
|
|
|
|
|
This frame’s modified region is cleared to fully transparent black before rendering
|
|
|
|
|
the next frame.
|
|
|
|
|
See :ref:`Saving APNG sequences<apng-saving>`.
|
|
|
|
|
"""
|
|
|
|
|
OP_PREVIOUS = 2
|
|
|
|
|
"""
|
|
|
|
|
This frame’s modified region is reverted to the previous frame’s contents before
|
|
|
|
|
rendering the next frame.
|
|
|
|
|
See :ref:`Saving APNG sequences<apng-saving>`.
|
|
|
|
|
"""
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
# APNG frame blend modes
|
2022-01-15 01:02:31 +03:00
|
|
|
|
class Blend(IntEnum):
|
|
|
|
|
OP_SOURCE = 0
|
|
|
|
|
"""
|
|
|
|
|
All color components of this frame, including alpha, overwrite the previous output
|
|
|
|
|
image contents.
|
|
|
|
|
See :ref:`Saving APNG sequences<apng-saving>`.
|
|
|
|
|
"""
|
|
|
|
|
OP_OVER = 1
|
|
|
|
|
"""
|
|
|
|
|
This frame should be alpha composited with the previous output image contents.
|
|
|
|
|
See :ref:`Saving APNG sequences<apng-saving>`.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2022-01-15 02:07:07 +03:00
|
|
|
|
def __getattr__(name):
|
|
|
|
|
for enum, prefix in {Disposal: "APNG_DISPOSE_", Blend: "APNG_BLEND_"}.items():
|
|
|
|
|
if name.startswith(prefix):
|
|
|
|
|
name = name[len(prefix) :]
|
|
|
|
|
if name in enum.__members__:
|
2022-04-05 16:33:20 +03:00
|
|
|
|
deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}")
|
2022-01-15 02:07:07 +03:00
|
|
|
|
return enum[name]
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"module '{__name__}' has no attribute '{name}'"
|
|
|
|
|
raise AttributeError(msg)
|
2019-12-01 06:50:21 +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:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "Decompressed Data Too Large"
|
|
|
|
|
raise ValueError(msg)
|
2014-12-01 10:31:29 +03:00
|
|
|
|
return plaintext
|
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
|
2018-01-05 18:53:50 +03:00
|
|
|
|
def _crc32(data, seed=0):
|
2019-03-21 16:28:20 +03:00
|
|
|
|
return zlib.crc32(data, seed) & 0xFFFFFFFF
|
2018-01-05 18:53:50 +03:00
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
|
class ChunkStream:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
self.fp = fp
|
|
|
|
|
self.queue = []
|
|
|
|
|
|
|
|
|
|
def read(self):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""Fetch a new chunk. Returns header information."""
|
2016-04-04 13:08:22 +03:00
|
|
|
|
cid = None
|
2016-04-04 15:40:37 +03:00
|
|
|
|
|
|
|
|
|
if self.queue:
|
2016-06-18 04:25:39 +03:00
|
|
|
|
cid, pos, length = self.queue.pop()
|
2016-04-04 15:40:37 +03:00
|
|
|
|
self.fp.seek(pos)
|
|
|
|
|
else:
|
|
|
|
|
s = self.fp.read(8)
|
|
|
|
|
cid = s[4:]
|
|
|
|
|
pos = self.fp.tell()
|
|
|
|
|
length = i32(s)
|
2016-04-19 10:52:01 +03:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
if not is_cid(cid):
|
2017-05-22 19:38:38 +03:00
|
|
|
|
if not ImageFile.LOAD_TRUNCATED_IMAGES:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"broken PNG file (chunk {repr(cid)})"
|
|
|
|
|
raise SyntaxError(msg)
|
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
|
|
|
|
|
2017-10-07 15:18:23 +03:00
|
|
|
|
def __enter__(self):
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
|
self.close()
|
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def close(self):
|
2018-01-03 13:56:17 +03:00
|
|
|
|
self.queue = self.fp = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2014-05-21 21:45:02 +04:00
|
|
|
|
def push(self, cid, pos, length):
|
|
|
|
|
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):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""Call the appropriate chunk handler"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2016-10-22 20:55:50 +03:00
|
|
|
|
logger.debug("STREAM %r %s %s", cid, pos, length)
|
2019-03-21 16:28:20 +03:00
|
|
|
|
return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
def crc(self, cid, data):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""Read and verify checksum"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2018-06-24 15:32:25 +03:00
|
|
|
|
# Skip CRC checks for ancillary chunks if allowed to load truncated
|
|
|
|
|
# images
|
2016-06-29 22:24:37 +03:00
|
|
|
|
# 5th byte of first char is 1 [specs, section 5.4]
|
2020-05-08 19:48:02 +03:00
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):
|
2016-06-29 22:24:37 +03:00
|
|
|
|
self.crc_skip(cid, data)
|
|
|
|
|
return
|
|
|
|
|
|
2016-04-04 13:08:22 +03:00
|
|
|
|
try:
|
2018-01-05 18:53:50 +03:00
|
|
|
|
crc1 = _crc32(data, _crc32(cid))
|
|
|
|
|
crc2 = i32(self.fp.read(4))
|
2016-04-04 13:08:22 +03:00
|
|
|
|
if crc1 != crc2:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"broken PNG file (bad header checksum in {repr(cid)})"
|
|
|
|
|
raise SyntaxError(msg)
|
2020-06-21 13:13:35 +03:00
|
|
|
|
except struct.error as e:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"broken PNG file (incomplete checksum in {repr(cid)})"
|
|
|
|
|
raise SyntaxError(msg) from e
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
def crc_skip(self, cid, data):
|
2022-09-30 14:57:54 +03:00
|
|
|
|
"""Read checksum"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
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:
|
2016-04-04 15:40:37 +03:00
|
|
|
|
try:
|
|
|
|
|
cid, pos, length = self.read()
|
2020-06-21 13:13:35 +03:00
|
|
|
|
except struct.error as e:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "truncated PNG file"
|
|
|
|
|
raise OSError(msg) from e
|
2016-04-04 15:40:37 +03:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
"""
|
2019-03-21 16:28:20 +03:00
|
|
|
|
|
2014-07-23 11:24:18 +04:00
|
|
|
|
@staticmethod
|
2019-01-02 10:56:54 +03:00
|
|
|
|
def __new__(cls, text, lang=None, tkey=None):
|
2014-11-20 02:35:01 +03:00
|
|
|
|
"""
|
2017-09-29 14:08:49 +03:00
|
|
|
|
:param cls: the class to use when creating the instance
|
|
|
|
|
:param text: value for this key
|
2014-11-20 02:35:01 +03:00
|
|
|
|
: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
|
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
|
class PngInfo:
|
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 = []
|
|
|
|
|
|
2020-03-11 13:40:17 +03:00
|
|
|
|
def add(self, cid, data, after_idat=False):
|
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
|
2020-03-11 13:40:17 +03:00
|
|
|
|
:param after_idat: for use with private chunks. Whether the chunk
|
|
|
|
|
should be written after IDAT
|
2014-11-20 02:35:01 +03:00
|
|
|
|
|
|
|
|
|
"""
|
2015-03-05 22:07:16 +03:00
|
|
|
|
|
2020-03-11 13:40:17 +03:00
|
|
|
|
chunk = [cid, data]
|
|
|
|
|
if after_idat:
|
|
|
|
|
chunk.append(True)
|
|
|
|
|
self.chunks.append(tuple(chunk))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
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:
|
2019-03-21 16:28:20 +03: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:
|
2019-03-21 16:28:20 +03: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
|
|
|
|
|
2017-12-16 20:13:45 +03:00
|
|
|
|
def add_text(self, key, value, zip=False):
|
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):
|
2017-12-16 20:13:45 +03:00
|
|
|
|
return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)
|
2014-07-23 11:24:18 +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
|
|
|
|
# The tEXt chunk stores latin-1 text
|
2014-07-23 18:43:52 +04:00
|
|
|
|
if not isinstance(value, bytes):
|
|
|
|
|
try:
|
2019-03-21 16:28:20 +03:00
|
|
|
|
value = value.encode("latin-1", "strict")
|
2014-07-23 18:43:52 +04:00
|
|
|
|
except UnicodeError:
|
2017-12-16 20:13:45 +03:00
|
|
|
|
return self.add_itxt(key, value, zip=zip)
|
2014-07-23 18:43:52 +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 not isinstance(key, bytes):
|
2019-03-21 16:28:20 +03:00
|
|
|
|
key = key.encode("latin-1", "strict")
|
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
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
class PngStream(ChunkStream):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def __init__(self, fp):
|
2016-11-05 20:31:11 +03:00
|
|
|
|
super().__init__(fp)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
# 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
|
2019-01-02 06:13:10 +03:00
|
|
|
|
self.im_custom_mimetype = None
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.im_n_frames = None
|
|
|
|
|
self._seq_num = None
|
|
|
|
|
self.rewind_state = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
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:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = (
|
2020-07-16 12:43:29 +03:00
|
|
|
|
"Too much memory used in text chunks: "
|
|
|
|
|
f"{self.text_memory}>MAX_TEXT_MEMORY"
|
2019-03-21 16:28:20 +03:00
|
|
|
|
)
|
2022-12-22 00:51:35 +03:00
|
|
|
|
raise ValueError(msg)
|
2014-12-30 04:10:27 +03:00
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
def save_rewind(self):
|
|
|
|
|
self.rewind_state = {
|
|
|
|
|
"info": self.im_info.copy(),
|
|
|
|
|
"tile": self.im_tile,
|
|
|
|
|
"seq_num": self._seq_num,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def rewind(self):
|
|
|
|
|
self.im_info = self.rewind_state["info"]
|
|
|
|
|
self.im_tile = self.rewind_state["tile"]
|
|
|
|
|
self._seq_num = self.rewind_state["seq_num"]
|
|
|
|
|
|
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")
|
2016-10-22 20:55:50 +03:00
|
|
|
|
logger.debug("iCCP profile name %r", s[:i])
|
2020-05-08 19:48:02 +03:00
|
|
|
|
logger.debug("Compression method %s", s[i])
|
|
|
|
|
comp_method = s[i]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
if comp_method != 0:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"Unknown compression method {comp_method} in iCCP chunk"
|
|
|
|
|
raise SyntaxError(msg)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
try:
|
2019-03-21 16:28:20 +03:00
|
|
|
|
icc_profile = _safe_zlib_decompress(s[i + 2 :])
|
2016-06-20 17:33:10 +03:00
|
|
|
|
except ValueError:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
icc_profile = None
|
|
|
|
|
else:
|
|
|
|
|
raise
|
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)
|
2022-05-01 06:45:58 +03:00
|
|
|
|
if length < 13:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "Truncated IHDR chunk"
|
|
|
|
|
raise ValueError(msg)
|
2020-05-08 21:11:58 +03:00
|
|
|
|
self.im_size = i32(s, 0), i32(s, 4)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
try:
|
2020-05-08 19:48:02 +03:00
|
|
|
|
self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
|
2018-11-17 00:51:52 +03:00
|
|
|
|
except Exception:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
pass
|
2020-05-08 19:48:02 +03:00
|
|
|
|
if s[12]:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
self.im_info["interlace"] = 1
|
2020-05-08 19:48:02 +03:00
|
|
|
|
if s[11]:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "unknown filter category"
|
|
|
|
|
raise SyntaxError(msg)
|
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
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if "bbox" in self.im_info:
|
2019-12-01 08:02:31 +03:00
|
|
|
|
tile = [("zip", self.im_info["bbox"], pos, self.im_rawmode)]
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
|
|
|
|
if self.im_n_frames is not None:
|
|
|
|
|
self.im_info["default_image"] = True
|
|
|
|
|
tile = [("zip", (0, 0) + self.im_size, pos, self.im_rawmode)]
|
|
|
|
|
self.im_tile = tile
|
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):
|
2016-04-19 10:52:01 +03:00
|
|
|
|
# tRNS contains only one full-transparent entry,
|
|
|
|
|
# other entries are full opaque
|
2013-03-11 23:33:04 +04:00
|
|
|
|
i = s.find(b"\0")
|
|
|
|
|
if i >= 0:
|
|
|
|
|
self.im_info["transparency"] = i
|
|
|
|
|
else:
|
2016-04-19 10:52:01 +03:00
|
|
|
|
# otherwise, we have a byte string with one alpha value
|
|
|
|
|
# for each palette entry
|
2013-03-11 23:33:04 +04:00
|
|
|
|
self.im_info["transparency"] = s
|
2019-03-26 23:41:33 +03:00
|
|
|
|
elif self.im_mode in ("1", "L", "I"):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
self.im_info["transparency"] = i16(s)
|
|
|
|
|
elif self.im_mode == "RGB":
|
2020-05-08 21:11:58 +03:00
|
|
|
|
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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
|
|
|
|
|
|
2017-12-20 14:28:50 +03:00
|
|
|
|
def chunk_cHRM(self, pos, length):
|
2017-12-20 15:34:42 +03:00
|
|
|
|
# chromaticity, 8 unsigned ints, actual value is scaled by 100,000
|
2017-12-20 14:28:50 +03:00
|
|
|
|
# WP x,y, Red x,y, Green x,y Blue x,y
|
|
|
|
|
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2019-03-21 16:28:20 +03:00
|
|
|
|
raw_vals = struct.unpack(">%dI" % (len(s) // 4), s)
|
|
|
|
|
self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals)
|
2017-12-20 14:28:50 +03:00
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def chunk_sRGB(self, pos, length):
|
|
|
|
|
# srgb rendering intent, 1 byte
|
|
|
|
|
# 0 perceptual
|
2017-12-20 14:46:01 +03:00
|
|
|
|
# 1 relative colorimetric
|
2017-12-20 14:28:50 +03:00
|
|
|
|
# 2 saturation
|
|
|
|
|
# 3 absolute colorimetric
|
|
|
|
|
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2022-07-11 13:02:41 +03:00
|
|
|
|
if length < 1:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "Truncated sRGB chunk"
|
|
|
|
|
raise ValueError(msg)
|
2020-05-08 19:48:02 +03:00
|
|
|
|
self.im_info["srgb"] = s[0]
|
2017-12-20 14:28:50 +03:00
|
|
|
|
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)
|
2022-05-01 06:45:58 +03:00
|
|
|
|
if length < 9:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "Truncated pHYs chunk"
|
|
|
|
|
raise ValueError(msg)
|
2020-05-08 21:11:58 +03:00
|
|
|
|
px, py = i32(s, 0), i32(s, 4)
|
2020-05-08 19:48:02 +03:00
|
|
|
|
unit = s[8]
|
2014-08-26 17:47:10 +04:00
|
|
|
|
if unit == 1: # meter
|
2021-05-07 13:39:05 +03:00
|
|
|
|
dpi = px * 0.0254, py * 0.0254
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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:
|
2019-09-26 15:12:28 +03:00
|
|
|
|
k = k.decode("latin-1", "strict")
|
2020-09-01 20:16:46 +03:00
|
|
|
|
v_str = 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
|
|
|
|
|
2020-09-01 20:16:46 +03:00
|
|
|
|
self.im_info[k] = v if k == "exif" else v_str
|
|
|
|
|
self.im_text[k] = v_str
|
|
|
|
|
self.check_text_memory(len(v_str))
|
2014-12-30 04:10:27 +03:00
|
|
|
|
|
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:
|
2020-05-08 19:48:02 +03:00
|
|
|
|
comp_method = v[0]
|
2013-08-20 16:17:17 +04:00
|
|
|
|
else:
|
|
|
|
|
comp_method = 0
|
2010-07-31 06:52:47 +04:00
|
|
|
|
if comp_method != 0:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"Unknown compression method {comp_method} in zTXt chunk"
|
|
|
|
|
raise SyntaxError(msg)
|
2013-08-20 16:17:17 +04:00
|
|
|
|
try:
|
2014-12-01 10:31:29 +03:00
|
|
|
|
v = _safe_zlib_decompress(v[1:])
|
2016-06-20 17:33:10 +03:00
|
|
|
|
except ValueError:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
v = b""
|
|
|
|
|
else:
|
|
|
|
|
raise
|
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:
|
2019-09-26 15:12:28 +03:00
|
|
|
|
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
|
2020-05-08 19:48:02 +03:00
|
|
|
|
cf, cm, r = r[0], r[1], r[2:]
|
2014-07-23 11:24:18 +04:00
|
|
|
|
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)
|
2016-06-20 17:33:10 +03:00
|
|
|
|
except ValueError:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
|
|
|
|
else:
|
|
|
|
|
raise
|
2014-07-23 11:24:18 +04:00
|
|
|
|
except zlib.error:
|
|
|
|
|
return s
|
|
|
|
|
else:
|
|
|
|
|
return s
|
2019-09-26 15:12:28 +03:00
|
|
|
|
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
|
2014-07-23 11:24:18 +04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2019-02-23 05:30:38 +03:00
|
|
|
|
def chunk_eXIf(self, pos, length):
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2019-03-21 16:28:20 +03:00
|
|
|
|
self.im_info["exif"] = b"Exif\x00\x00" + s
|
2019-02-23 05:30:38 +03:00
|
|
|
|
return s
|
|
|
|
|
|
2018-12-27 02:53:16 +03:00
|
|
|
|
# APNG chunks
|
|
|
|
|
def chunk_acTL(self, pos, length):
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2022-05-01 06:45:58 +03:00
|
|
|
|
if length < 8:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains truncated acTL chunk"
|
|
|
|
|
raise ValueError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if self.im_n_frames is not None:
|
|
|
|
|
self.im_n_frames = None
|
|
|
|
|
warnings.warn("Invalid APNG, will use default PNG image if possible")
|
|
|
|
|
return s
|
|
|
|
|
n_frames = i32(s)
|
|
|
|
|
if n_frames == 0 or n_frames > 0x80000000:
|
|
|
|
|
warnings.warn("Invalid APNG, will use default PNG image if possible")
|
|
|
|
|
return s
|
|
|
|
|
self.im_n_frames = n_frames
|
2020-05-08 21:11:58 +03:00
|
|
|
|
self.im_info["loop"] = i32(s, 4)
|
2019-03-21 16:28:20 +03:00
|
|
|
|
self.im_custom_mimetype = "image/apng"
|
2018-12-27 02:53:16 +03:00
|
|
|
|
return s
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
def chunk_fcTL(self, pos, length):
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2022-05-01 06:45:58 +03:00
|
|
|
|
if length < 26:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains truncated fcTL chunk"
|
|
|
|
|
raise ValueError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
seq = i32(s)
|
2019-12-01 08:02:31 +03:00
|
|
|
|
if (self._seq_num is None and seq != 0) or (
|
|
|
|
|
self._seq_num is not None and self._seq_num != seq - 1
|
|
|
|
|
):
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains frame sequence errors"
|
|
|
|
|
raise SyntaxError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self._seq_num = seq
|
2020-05-08 21:11:58 +03:00
|
|
|
|
width, height = i32(s, 4), i32(s, 8)
|
|
|
|
|
px, py = i32(s, 12), i32(s, 16)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
im_w, im_h = self.im_size
|
|
|
|
|
if px + width > im_w or py + height > im_h:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains invalid frames"
|
|
|
|
|
raise SyntaxError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.im_info["bbox"] = (px, py, px + width, py + height)
|
2020-05-08 21:11:58 +03:00
|
|
|
|
delay_num, delay_den = i16(s, 20), i16(s, 22)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if delay_den == 0:
|
|
|
|
|
delay_den = 100
|
|
|
|
|
self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000
|
2020-05-08 19:48:02 +03:00
|
|
|
|
self.im_info["disposal"] = s[24]
|
|
|
|
|
self.im_info["blend"] = s[25]
|
2019-12-01 06:50:21 +03:00
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def chunk_fdAT(self, pos, length):
|
2022-05-01 06:45:58 +03:00
|
|
|
|
if length < 4:
|
|
|
|
|
if ImageFile.LOAD_TRUNCATED_IMAGES:
|
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
|
|
|
|
return s
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains truncated fDAT chunk"
|
|
|
|
|
raise ValueError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
s = ImageFile._safe_read(self.fp, 4)
|
|
|
|
|
seq = i32(s)
|
|
|
|
|
if self._seq_num != seq - 1:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG contains frame sequence errors"
|
|
|
|
|
raise SyntaxError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self._seq_num = seq
|
|
|
|
|
return self.chunk_IDAT(pos + 4, length - 4)
|
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
# PNG reader
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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.
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
class PngImageFile(ImageFile.ImageFile):
|
|
|
|
|
format = "PNG"
|
|
|
|
|
format_description = "Portable network graphics"
|
|
|
|
|
|
|
|
|
|
def _open(self):
|
2020-06-23 10:41:13 +03:00
|
|
|
|
if not _accept(self.fp.read(8)):
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "not a PNG file"
|
|
|
|
|
raise SyntaxError(msg)
|
2022-04-13 02:54:17 +03:00
|
|
|
|
self._fp = self.fp
|
2020-04-02 08:49:26 +03:00
|
|
|
|
self.__frame = 0
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
#
|
2019-12-01 10:30:34 +03:00
|
|
|
|
# Parse headers up to the first IDAT or fDAT chunk
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2020-03-11 13:40:17 +03:00
|
|
|
|
self.private_chunks = []
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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:
|
2016-10-22 20:55:50 +03:00
|
|
|
|
logger.debug("%r %s %s (unknown)", cid, pos, length)
|
2014-05-21 21:45:02 +04:00
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
2020-03-11 13:40:17 +03:00
|
|
|
|
if cid[1:2].islower():
|
|
|
|
|
self.private_chunks.append((cid, s))
|
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
|
2018-09-30 05:58:02 +03:00
|
|
|
|
self._size = self.png.im_size
|
2010-07-31 06:52:47 +04:00
|
|
|
|
self.info = self.png.im_info
|
2018-12-24 15:58:19 +03:00
|
|
|
|
self._text = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
self.tile = self.png.im_tile
|
2019-01-02 06:13:10 +03:00
|
|
|
|
self.custom_mimetype = self.png.im_custom_mimetype
|
2020-04-05 08:23:18 +03:00
|
|
|
|
self.n_frames = self.png.im_n_frames or 1
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.default_image = self.info.get("default_image", False)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
if self.png.im_palette:
|
|
|
|
|
rawmode, data = self.png.im_palette
|
|
|
|
|
self.palette = ImagePalette.raw(rawmode, data)
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if cid == b"fdAT":
|
|
|
|
|
self.__prepare_idat = length - 4
|
|
|
|
|
else:
|
2019-12-01 08:02:31 +03:00
|
|
|
|
self.__prepare_idat = length # used by load_prepare()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-04-05 08:23:18 +03:00
|
|
|
|
if self.png.im_n_frames is not None:
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self._close_exclusive_fp_after_loading = False
|
|
|
|
|
self.png.save_rewind()
|
|
|
|
|
self.__rewind_idat = self.__prepare_idat
|
2022-04-13 02:54:17 +03:00
|
|
|
|
self.__rewind = self._fp.tell()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if self.default_image:
|
|
|
|
|
# IDAT chunk contains default image and not first animation frame
|
2020-04-05 08:23:18 +03:00
|
|
|
|
self.n_frames += 1
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self._seek(0)
|
2020-04-05 08:23:18 +03:00
|
|
|
|
self.is_animated = self.n_frames > 1
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2018-12-24 15:58:19 +03:00
|
|
|
|
@property
|
|
|
|
|
def text(self):
|
|
|
|
|
# experimental
|
|
|
|
|
if self._text is None:
|
|
|
|
|
# iTxt, tEXt and zTXt chunks may appear at the end of the file
|
|
|
|
|
# So load the file to ensure that they are read
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if self.is_animated:
|
|
|
|
|
frame = self.__frame
|
|
|
|
|
# for APNG, seek to the final frame before loading
|
|
|
|
|
self.seek(self.n_frames - 1)
|
2018-12-24 15:58:19 +03:00
|
|
|
|
self.load()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if self.is_animated:
|
|
|
|
|
self.seek(frame)
|
2018-12-24 15:58:19 +03:00
|
|
|
|
return self._text
|
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def verify(self):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""Verify PNG file"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
if self.fp is None:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "verify must be called directly after open"
|
|
|
|
|
raise RuntimeError(msg)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
# back up to beginning of IDAT block
|
|
|
|
|
self.fp.seek(self.tile[0][2] - 8)
|
|
|
|
|
|
|
|
|
|
self.png.verify()
|
|
|
|
|
self.png.close()
|
|
|
|
|
|
2018-11-13 14:13:55 +03:00
|
|
|
|
if self._exclusive_fp:
|
|
|
|
|
self.fp.close()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
self.fp = None
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
def seek(self, frame):
|
|
|
|
|
if not self._seek_check(frame):
|
|
|
|
|
return
|
|
|
|
|
if frame < self.__frame:
|
|
|
|
|
self._seek(0, True)
|
|
|
|
|
|
|
|
|
|
last_frame = self.__frame
|
|
|
|
|
for f in range(self.__frame + 1, frame + 1):
|
|
|
|
|
try:
|
|
|
|
|
self._seek(f)
|
2020-06-21 13:13:35 +03:00
|
|
|
|
except EOFError as e:
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.seek(last_frame)
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "no more images in APNG file"
|
|
|
|
|
raise EOFError(msg) from e
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
def _seek(self, frame, rewind=False):
|
|
|
|
|
if frame == 0:
|
|
|
|
|
if rewind:
|
2022-04-13 02:54:17 +03:00
|
|
|
|
self._fp.seek(self.__rewind)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.png.rewind()
|
|
|
|
|
self.__prepare_idat = self.__rewind_idat
|
|
|
|
|
self.im = None
|
2019-12-01 09:50:09 +03:00
|
|
|
|
if self.pyaccess:
|
|
|
|
|
self.pyaccess = None
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.info = self.png.im_info
|
|
|
|
|
self.tile = self.png.im_tile
|
2022-04-13 02:54:17 +03:00
|
|
|
|
self.fp = self._fp
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self._prev_im = None
|
|
|
|
|
self.dispose = None
|
|
|
|
|
self.default_image = self.info.get("default_image", False)
|
|
|
|
|
self.dispose_op = self.info.get("disposal")
|
|
|
|
|
self.blend_op = self.info.get("blend")
|
|
|
|
|
self.dispose_extent = self.info.get("bbox")
|
|
|
|
|
self.__frame = 0
|
|
|
|
|
else:
|
|
|
|
|
if frame != self.__frame + 1:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"cannot seek to frame {frame}"
|
|
|
|
|
raise ValueError(msg)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
# ensure previous frame was loaded
|
|
|
|
|
self.load()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
if self.dispose:
|
|
|
|
|
self.im.paste(self.dispose, self.dispose_extent)
|
|
|
|
|
self._prev_im = self.im.copy()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2022-04-13 02:54:17 +03:00
|
|
|
|
self.fp = self._fp
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
# advance to the next frame
|
|
|
|
|
if self.__prepare_idat:
|
|
|
|
|
ImageFile._safe_read(self.fp, self.__prepare_idat)
|
|
|
|
|
self.__prepare_idat = 0
|
|
|
|
|
frame_start = False
|
|
|
|
|
while True:
|
|
|
|
|
self.fp.read(4) # CRC
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
try:
|
|
|
|
|
cid, pos, length = self.png.read()
|
|
|
|
|
except (struct.error, SyntaxError):
|
|
|
|
|
break
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
if cid == b"IEND":
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "No more images in APNG file"
|
|
|
|
|
raise EOFError(msg)
|
2020-12-24 01:55:22 +03:00
|
|
|
|
if cid == b"fcTL":
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if frame_start:
|
2020-12-24 01:55:22 +03:00
|
|
|
|
# there must be at least one fdAT chunk between fcTL chunks
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "APNG missing frame data"
|
|
|
|
|
raise SyntaxError(msg)
|
2020-12-24 01:55:22 +03:00
|
|
|
|
frame_start = True
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2020-12-24 01:55:22 +03:00
|
|
|
|
try:
|
|
|
|
|
self.png.call(cid, pos, length)
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
break
|
|
|
|
|
except EOFError:
|
|
|
|
|
if cid == b"fdAT":
|
|
|
|
|
length -= 4
|
|
|
|
|
if frame_start:
|
|
|
|
|
self.__prepare_idat = length
|
|
|
|
|
break
|
|
|
|
|
ImageFile._safe_read(self.fp, length)
|
|
|
|
|
except AttributeError:
|
|
|
|
|
logger.debug("%r %s %s (unknown)", cid, pos, length)
|
|
|
|
|
ImageFile._safe_read(self.fp, length)
|
|
|
|
|
|
|
|
|
|
self.__frame = frame
|
|
|
|
|
self.tile = self.png.im_tile
|
|
|
|
|
self.dispose_op = self.info.get("disposal")
|
|
|
|
|
self.blend_op = self.info.get("blend")
|
|
|
|
|
self.dispose_extent = self.info.get("bbox")
|
|
|
|
|
|
|
|
|
|
if not self.tile:
|
|
|
|
|
raise EOFError
|
|
|
|
|
|
|
|
|
|
# setup frame disposal (actual disposal done when needed in the next _seek())
|
2022-01-15 01:02:31 +03:00
|
|
|
|
if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:
|
|
|
|
|
self.dispose_op = Disposal.OP_BACKGROUND
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2022-01-15 01:02:31 +03:00
|
|
|
|
if self.dispose_op == Disposal.OP_PREVIOUS:
|
2020-12-24 01:55:22 +03:00
|
|
|
|
self.dispose = self._prev_im.copy()
|
|
|
|
|
self.dispose = self._crop(self.dispose, self.dispose_extent)
|
2022-01-15 01:02:31 +03:00
|
|
|
|
elif self.dispose_op == Disposal.OP_BACKGROUND:
|
2020-12-24 01:55:22 +03:00
|
|
|
|
self.dispose = Image.core.fill(self.mode, self.size)
|
|
|
|
|
self.dispose = self._crop(self.dispose, self.dispose_extent)
|
|
|
|
|
else:
|
|
|
|
|
self.dispose = None
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
|
return self.__frame
|
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def load_prepare(self):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""internal: prepare to read PNG file"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
if self.info.get("interlace"):
|
|
|
|
|
self.decoderconfig = self.decoderconfig + (1,)
|
|
|
|
|
|
2019-07-13 01:37:17 +03:00
|
|
|
|
self.__idat = self.__prepare_idat # used by load_read()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
ImageFile.ImageFile.load_prepare(self)
|
|
|
|
|
|
2014-05-21 22:33:49 +04:00
|
|
|
|
def load_read(self, read_bytes):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""internal: read more image data"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if cid not in [b"IDAT", b"DDAT", b"fdAT"]:
|
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
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if cid == b"fdAT":
|
|
|
|
|
try:
|
|
|
|
|
self.png.call(cid, pos, length)
|
|
|
|
|
except EOFError:
|
|
|
|
|
pass
|
2019-12-01 08:02:31 +03:00
|
|
|
|
self.__idat = length - 4 # sequence_num has already been read
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
2019-12-01 08:02:31 +03: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):
|
2019-02-03 07:58:24 +03:00
|
|
|
|
"""internal: finished reading image data"""
|
2021-05-14 06:18:49 +03:00
|
|
|
|
if self.__idat != 0:
|
|
|
|
|
self.fp.read(self.__idat)
|
2018-12-24 15:58:19 +03:00
|
|
|
|
while True:
|
|
|
|
|
self.fp.read(4) # CRC
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
cid, pos, length = self.png.read()
|
|
|
|
|
except (struct.error, SyntaxError):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if cid == b"IEND":
|
|
|
|
|
break
|
2019-12-01 06:50:21 +03:00
|
|
|
|
elif cid == b"fcTL" and self.is_animated:
|
|
|
|
|
# start of the next frame, stop reading
|
|
|
|
|
self.__prepare_idat = 0
|
|
|
|
|
self.png.push(cid, pos, length)
|
|
|
|
|
break
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2018-12-24 15:58:19 +03:00
|
|
|
|
try:
|
|
|
|
|
self.png.call(cid, pos, length)
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
break
|
2019-01-03 11:13:19 +03:00
|
|
|
|
except EOFError:
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if cid == b"fdAT":
|
|
|
|
|
length -= 4
|
2019-01-03 11:13:19 +03:00
|
|
|
|
ImageFile._safe_read(self.fp, length)
|
2019-01-07 23:21:25 +03:00
|
|
|
|
except AttributeError:
|
|
|
|
|
logger.debug("%r %s %s (unknown)", cid, pos, length)
|
2020-03-11 13:40:17 +03:00
|
|
|
|
s = ImageFile._safe_read(self.fp, length)
|
|
|
|
|
if cid[1:2].islower():
|
|
|
|
|
self.private_chunks.append((cid, s, True))
|
2018-12-24 15:58:19 +03:00
|
|
|
|
self._text = self.png.im_text
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if not self.is_animated:
|
|
|
|
|
self.png.close()
|
|
|
|
|
self.png = None
|
|
|
|
|
else:
|
2022-01-15 01:02:31 +03:00
|
|
|
|
if self._prev_im and self.blend_op == Blend.OP_OVER:
|
2019-12-01 06:50:21 +03:00
|
|
|
|
updated = self._crop(self.im, self.dispose_extent)
|
2023-03-16 12:05:56 +03:00
|
|
|
|
if self.im.mode == "RGB" and "transparency" in self.info:
|
|
|
|
|
mask = updated.convert_transparent(
|
|
|
|
|
"RGBA", self.info["transparency"]
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
mask = updated.convert("RGBA")
|
|
|
|
|
self._prev_im.paste(updated, self.dispose_extent, mask)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
self.im = self._prev_im
|
2019-12-01 09:50:09 +03:00
|
|
|
|
if self.pyaccess:
|
|
|
|
|
self.pyaccess = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2019-02-23 05:30:38 +03:00
|
|
|
|
def _getexif(self):
|
2019-03-11 13:18:36 +03:00
|
|
|
|
if "exif" not in self.info:
|
|
|
|
|
self.load()
|
2020-03-13 16:04:58 +03:00
|
|
|
|
if "exif" not in self.info and "Raw profile type exif" not in self.info:
|
2019-03-12 02:27:43 +03:00
|
|
|
|
return None
|
2020-10-05 12:35:45 +03:00
|
|
|
|
return self.getexif()._get_merged_dict()
|
2019-03-12 02:27:43 +03:00
|
|
|
|
|
|
|
|
|
def getexif(self):
|
|
|
|
|
if "exif" not in self.info:
|
|
|
|
|
self.load()
|
2020-03-13 16:04:58 +03:00
|
|
|
|
|
2020-04-16 14:14:19 +03:00
|
|
|
|
return super().getexif()
|
2019-02-23 05:30:38 +03:00
|
|
|
|
|
2021-06-12 06:57:14 +03:00
|
|
|
|
def getxmp(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns a dictionary containing the XMP tags.
|
2021-06-30 04:28:00 +03:00
|
|
|
|
Requires defusedxml to be installed.
|
2022-02-09 15:28:43 +03:00
|
|
|
|
|
2021-06-12 06:57:14 +03:00
|
|
|
|
:returns: XMP tags in a dictionary.
|
|
|
|
|
"""
|
|
|
|
|
return (
|
|
|
|
|
self._getxmp(self.info["XML:com.adobe.xmp"])
|
|
|
|
|
if "XML:com.adobe.xmp" in self.info
|
|
|
|
|
else {}
|
|
|
|
|
)
|
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
# PNG writer
|
|
|
|
|
|
|
|
|
|
_OUTMODES = {
|
|
|
|
|
# supported PIL modes, and corresponding rawmodes/bits/color combinations
|
2019-03-21 16:28:20 +03: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"),
|
|
|
|
|
"I;16": ("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):
|
2018-04-09 16:13:19 +03:00
|
|
|
|
"""Write a PNG chunk (including CRC field)"""
|
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
|
|
|
|
data = b"".join(data)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
fp.write(o32(len(data)) + cid)
|
|
|
|
|
fp.write(data)
|
2018-01-05 18:53:50 +03:00
|
|
|
|
crc = _crc32(data, _crc32(cid))
|
|
|
|
|
fp.write(o32(crc))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
|
class _idat:
|
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
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
class _fdat:
|
|
|
|
|
# wrap encoder output in fdAT chunks
|
|
|
|
|
|
|
|
|
|
def __init__(self, fp, chunk, seq_num):
|
|
|
|
|
self.fp = fp
|
|
|
|
|
self.chunk = chunk
|
|
|
|
|
self.seq_num = seq_num
|
|
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
|
self.chunk(self.fp, b"fdAT", o32(self.seq_num), data)
|
2019-12-02 08:40:53 +03:00
|
|
|
|
self.seq_num += 1
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
|
2022-09-23 13:06:08 +03:00
|
|
|
|
def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):
|
2019-12-01 06:50:21 +03:00
|
|
|
|
duration = im.encoderinfo.get("duration", im.info.get("duration", 0))
|
|
|
|
|
loop = im.encoderinfo.get("loop", im.info.get("loop", 0))
|
2022-01-15 01:02:31 +03:00
|
|
|
|
disposal = im.encoderinfo.get("disposal", im.info.get("disposal", Disposal.OP_NONE))
|
|
|
|
|
blend = im.encoderinfo.get("blend", im.info.get("blend", Blend.OP_SOURCE))
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
if default_image:
|
2022-09-23 13:06:08 +03:00
|
|
|
|
chain = itertools.chain(append_images)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
2022-09-23 13:06:08 +03:00
|
|
|
|
chain = itertools.chain([im], append_images)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
im_frames = []
|
|
|
|
|
frame_count = 0
|
|
|
|
|
for im_seq in chain:
|
|
|
|
|
for im_frame in ImageSequence.Iterator(im_seq):
|
2022-09-23 13:06:08 +03:00
|
|
|
|
if im_frame.mode == rawmode:
|
|
|
|
|
im_frame = im_frame.copy()
|
|
|
|
|
else:
|
|
|
|
|
if rawmode == "P":
|
|
|
|
|
im_frame = im_frame.convert(rawmode, palette=im.palette)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
2022-09-23 13:06:08 +03:00
|
|
|
|
im_frame = im_frame.convert(rawmode)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
encoderinfo = im.encoderinfo.copy()
|
|
|
|
|
if isinstance(duration, (list, tuple)):
|
|
|
|
|
encoderinfo["duration"] = duration[frame_count]
|
|
|
|
|
if isinstance(disposal, (list, tuple)):
|
|
|
|
|
encoderinfo["disposal"] = disposal[frame_count]
|
|
|
|
|
if isinstance(blend, (list, tuple)):
|
|
|
|
|
encoderinfo["blend"] = blend[frame_count]
|
|
|
|
|
frame_count += 1
|
|
|
|
|
|
|
|
|
|
if im_frames:
|
|
|
|
|
previous = im_frames[-1]
|
|
|
|
|
prev_disposal = previous["encoderinfo"].get("disposal")
|
|
|
|
|
prev_blend = previous["encoderinfo"].get("blend")
|
2022-01-15 01:02:31 +03:00
|
|
|
|
if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:
|
|
|
|
|
prev_disposal = Disposal.OP_BACKGROUND
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
2022-01-15 01:02:31 +03:00
|
|
|
|
if prev_disposal == Disposal.OP_BACKGROUND:
|
2022-10-24 14:19:22 +03:00
|
|
|
|
base_im = previous["im"].copy()
|
2019-12-01 06:50:21 +03:00
|
|
|
|
dispose = Image.core.fill("RGBA", im.size, (0, 0, 0, 0))
|
|
|
|
|
bbox = previous["bbox"]
|
|
|
|
|
if bbox:
|
|
|
|
|
dispose = dispose.crop(bbox)
|
|
|
|
|
else:
|
|
|
|
|
bbox = (0, 0) + im.size
|
|
|
|
|
base_im.paste(dispose, bbox)
|
2022-01-15 01:02:31 +03:00
|
|
|
|
elif prev_disposal == Disposal.OP_PREVIOUS:
|
2019-12-01 06:50:21 +03:00
|
|
|
|
base_im = im_frames[-2]["im"]
|
|
|
|
|
else:
|
|
|
|
|
base_im = previous["im"]
|
2020-03-15 14:02:10 +03:00
|
|
|
|
delta = ImageChops.subtract_modulo(
|
|
|
|
|
im_frame.convert("RGB"), base_im.convert("RGB")
|
|
|
|
|
)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
bbox = delta.getbbox()
|
2019-12-01 08:02:31 +03:00
|
|
|
|
if (
|
|
|
|
|
not bbox
|
|
|
|
|
and prev_disposal == encoderinfo.get("disposal")
|
|
|
|
|
and prev_blend == encoderinfo.get("blend")
|
|
|
|
|
):
|
2021-07-13 15:59:49 +03:00
|
|
|
|
if isinstance(duration, (list, tuple)):
|
|
|
|
|
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
|
2019-12-01 06:50:21 +03:00
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
bbox = None
|
|
|
|
|
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
|
|
|
|
|
|
|
|
|
# animation control
|
|
|
|
|
chunk(
|
2020-09-01 20:16:46 +03:00
|
|
|
|
fp,
|
|
|
|
|
b"acTL",
|
|
|
|
|
o32(len(im_frames)), # 0: num_frames
|
|
|
|
|
o32(loop), # 4: num_plays
|
2019-12-01 06:50:21 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# default image IDAT (if it exists)
|
|
|
|
|
if default_image:
|
|
|
|
|
ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
|
|
|
|
|
|
|
|
|
|
seq_num = 0
|
|
|
|
|
for frame, frame_data in enumerate(im_frames):
|
|
|
|
|
im_frame = frame_data["im"]
|
|
|
|
|
if not frame_data["bbox"]:
|
|
|
|
|
bbox = (0, 0) + im_frame.size
|
|
|
|
|
else:
|
|
|
|
|
bbox = frame_data["bbox"]
|
|
|
|
|
im_frame = im_frame.crop(bbox)
|
|
|
|
|
size = im_frame.size
|
2021-07-13 16:02:23 +03:00
|
|
|
|
encoderinfo = frame_data["encoderinfo"]
|
|
|
|
|
frame_duration = int(round(encoderinfo.get("duration", duration)))
|
|
|
|
|
frame_disposal = encoderinfo.get("disposal", disposal)
|
|
|
|
|
frame_blend = encoderinfo.get("blend", blend)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
# frame control
|
|
|
|
|
chunk(
|
|
|
|
|
fp,
|
|
|
|
|
b"fcTL",
|
2019-12-01 08:02:31 +03:00
|
|
|
|
o32(seq_num), # sequence_number
|
|
|
|
|
o32(size[0]), # width
|
|
|
|
|
o32(size[1]), # height
|
|
|
|
|
o32(bbox[0]), # x_offset
|
|
|
|
|
o32(bbox[1]), # y_offset
|
2021-07-13 16:02:23 +03:00
|
|
|
|
o16(frame_duration), # delay_numerator
|
2019-12-01 08:02:31 +03:00
|
|
|
|
o16(1000), # delay_denominator
|
2021-07-13 16:02:23 +03:00
|
|
|
|
o8(frame_disposal), # dispose_op
|
|
|
|
|
o8(frame_blend), # blend_op
|
2019-12-01 06:50:21 +03:00
|
|
|
|
)
|
|
|
|
|
seq_num += 1
|
|
|
|
|
# frame data
|
|
|
|
|
if frame == 0 and not default_image:
|
|
|
|
|
# first frame must be in IDAT chunks for backwards compatibility
|
2019-12-01 08:02:31 +03:00
|
|
|
|
ImageFile._save(
|
|
|
|
|
im_frame,
|
|
|
|
|
_idat(fp, chunk),
|
|
|
|
|
[("zip", (0, 0) + im_frame.size, 0, rawmode)],
|
|
|
|
|
)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
2019-12-02 08:40:53 +03:00
|
|
|
|
fdat_chunks = _fdat(fp, chunk, seq_num)
|
2019-12-01 08:02:31 +03:00
|
|
|
|
ImageFile._save(
|
2020-09-01 20:16:46 +03:00
|
|
|
|
im_frame,
|
|
|
|
|
fdat_chunks,
|
|
|
|
|
[("zip", (0, 0) + im_frame.size, 0, rawmode)],
|
2019-12-01 08:02:31 +03:00
|
|
|
|
)
|
2019-12-02 08:40:53 +03:00
|
|
|
|
seq_num = fdat_chunks.seq_num
|
2019-12-01 06:50:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _save_all(im, fp, filename):
|
|
|
|
|
_save(im, fp, filename, save_all=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _save(im, fp, filename, chunk=putchunk, save_all=False):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
# save an image to disk (called by the save method)
|
|
|
|
|
|
2022-09-23 13:06:08 +03:00
|
|
|
|
if save_all:
|
|
|
|
|
default_image = im.encoderinfo.get(
|
|
|
|
|
"default_image", im.info.get("default_image")
|
|
|
|
|
)
|
|
|
|
|
modes = set()
|
|
|
|
|
append_images = im.encoderinfo.get("append_images", [])
|
|
|
|
|
if default_image:
|
|
|
|
|
chain = itertools.chain(append_images)
|
|
|
|
|
else:
|
|
|
|
|
chain = itertools.chain([im], append_images)
|
|
|
|
|
for im_seq in chain:
|
|
|
|
|
for im_frame in ImageSequence.Iterator(im_seq):
|
|
|
|
|
modes.add(im_frame.mode)
|
|
|
|
|
for mode in ("RGBA", "RGB", "P"):
|
|
|
|
|
if mode in modes:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
mode = modes.pop()
|
|
|
|
|
else:
|
|
|
|
|
mode = im.mode
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
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
|
2021-03-21 01:22:01 +03:00
|
|
|
|
colors = min(1 << im.encoderinfo["bits"], 256)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
else:
|
|
|
|
|
# check palette contents
|
2013-08-02 18:32:49 +04:00
|
|
|
|
if im.palette:
|
2021-03-21 01:22:01 +03:00
|
|
|
|
colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)
|
2013-08-02 18:32:49 +04:00
|
|
|
|
else:
|
|
|
|
|
colors = 256
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2021-03-21 01:22:01 +03:00
|
|
|
|
if colors <= 16:
|
|
|
|
|
if colors <= 2:
|
|
|
|
|
bits = 1
|
|
|
|
|
elif colors <= 4:
|
|
|
|
|
bits = 2
|
|
|
|
|
else:
|
|
|
|
|
bits = 4
|
2020-07-16 12:43:29 +03:00
|
|
|
|
mode = f"{mode};{bits}"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
# encoder options
|
2019-03-21 16:28:20 +03:00
|
|
|
|
im.encoderconfig = (
|
|
|
|
|
im.encoderinfo.get("optimize", False),
|
|
|
|
|
im.encoderinfo.get("compress_level", -1),
|
|
|
|
|
im.encoderinfo.get("compress_type", -1),
|
|
|
|
|
im.encoderinfo.get("dictionary", b""),
|
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
# get the corresponding PNG mode
|
|
|
|
|
try:
|
|
|
|
|
rawmode, mode = _OUTMODES[mode]
|
2020-06-21 13:13:35 +03:00
|
|
|
|
except KeyError as e:
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = f"cannot write mode {mode} as PNG"
|
|
|
|
|
raise OSError(msg) from e
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# write minimal PNG file
|
|
|
|
|
|
|
|
|
|
fp.write(_MAGIC)
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
chunk(
|
|
|
|
|
fp,
|
|
|
|
|
b"IHDR",
|
|
|
|
|
o32(im.size[0]), # 0: size
|
|
|
|
|
o32(im.size[1]),
|
|
|
|
|
mode, # 8: depth/type
|
|
|
|
|
b"\0", # 10: compression
|
|
|
|
|
b"\0", # 11: filter category
|
|
|
|
|
b"\0", # 12: interlace flag
|
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2017-01-21 08:57:03 +03:00
|
|
|
|
chunks = [b"cHRM", b"gAMA", b"sBIT", b"sRGB", b"tIME"]
|
|
|
|
|
|
2017-01-15 09:36:59 +03:00
|
|
|
|
icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
|
|
|
|
|
if icc:
|
|
|
|
|
# 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)
|
|
|
|
|
name = b"ICC Profile"
|
|
|
|
|
data = name + b"\0\0" + zlib.compress(icc)
|
|
|
|
|
chunk(fp, b"iCCP", data)
|
2018-01-27 09:02:56 +03:00
|
|
|
|
|
2017-10-05 10:54:18 +03:00
|
|
|
|
# You must either have sRGB or iCCP.
|
2017-12-20 15:34:42 +03:00
|
|
|
|
# Disallow sRGB chunks when an iCCP-chunk has been emitted.
|
2017-01-21 08:57:03 +03:00
|
|
|
|
chunks.remove(b"sRGB")
|
|
|
|
|
|
|
|
|
|
info = im.encoderinfo.get("pnginfo")
|
|
|
|
|
if info:
|
|
|
|
|
chunks_multiple_allowed = [b"sPLT", b"iTXt", b"tEXt", b"zTXt"]
|
2020-03-11 13:40:17 +03:00
|
|
|
|
for info_chunk in info.chunks:
|
|
|
|
|
cid, data = info_chunk[:2]
|
2017-01-21 08:57:03 +03:00
|
|
|
|
if cid in chunks:
|
|
|
|
|
chunks.remove(cid)
|
|
|
|
|
chunk(fp, cid, data)
|
|
|
|
|
elif cid in chunks_multiple_allowed:
|
|
|
|
|
chunk(fp, cid, data)
|
2020-03-11 13:40:17 +03:00
|
|
|
|
elif cid[1:2].islower():
|
|
|
|
|
# Private chunk
|
|
|
|
|
after_idat = info_chunk[2:3]
|
|
|
|
|
if not after_idat:
|
|
|
|
|
chunk(fp, cid, data)
|
2017-01-15 09:36:59 +03:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
if im.mode == "P":
|
2021-03-21 01:22:01 +03:00
|
|
|
|
palette_byte_number = colors * 3
|
2013-08-02 18:32:49 +04:00
|
|
|
|
palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
|
|
|
|
|
while len(palette_bytes) < palette_byte_number:
|
2019-03-21 16:28:20 +03:00
|
|
|
|
palette_bytes += b"\0"
|
2013-08-02 18:32:49 +04:00
|
|
|
|
chunk(fp, b"PLTE", palette_bytes)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2019-03-21 16:28:20 +03: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
|
2021-03-21 01:22:01 +03:00
|
|
|
|
alpha_bytes = colors
|
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))
|
2019-03-21 16:28:20 +03:00
|
|
|
|
alpha = b"\xFF" * transparency + b"\0"
|
2013-03-21 21:43:22 +04:00
|
|
|
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
2019-03-26 23:41:33 +03:00
|
|
|
|
elif im.mode in ("1", "L", "I"):
|
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.
|
2022-12-22 00:51:35 +03:00
|
|
|
|
msg = "cannot use transparency for this mode"
|
|
|
|
|
raise OSError(msg)
|
2013-03-11 23:33:04 +04:00
|
|
|
|
else:
|
|
|
|
|
if im.mode == "P" and im.im.getpalettemode() == "RGBA":
|
|
|
|
|
alpha = im.im.getpalette("RGBA", "A")
|
2021-03-21 01:22:01 +03:00
|
|
|
|
alpha_bytes = colors
|
2013-03-11 23:58:54 +04:00
|
|
|
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
dpi = im.encoderinfo.get("dpi")
|
|
|
|
|
if dpi:
|
2019-03-21 16:28:20 +03:00
|
|
|
|
chunk(
|
|
|
|
|
fp,
|
|
|
|
|
b"pHYs",
|
|
|
|
|
o32(int(dpi[0] / 0.0254 + 0.5)),
|
|
|
|
|
o32(int(dpi[1] / 0.0254 + 0.5)),
|
|
|
|
|
b"\x01",
|
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
if info:
|
2017-01-21 08:57:03 +03:00
|
|
|
|
chunks = [b"bKGD", b"hIST"]
|
2020-03-11 13:40:17 +03:00
|
|
|
|
for info_chunk in info.chunks:
|
|
|
|
|
cid, data = info_chunk[:2]
|
2017-01-21 08:57:03 +03:00
|
|
|
|
if cid in chunks:
|
|
|
|
|
chunks.remove(cid)
|
|
|
|
|
chunk(fp, cid, data)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2022-12-23 04:10:36 +03:00
|
|
|
|
exif = im.encoderinfo.get("exif")
|
2019-02-23 05:30:38 +03:00
|
|
|
|
if exif:
|
2019-04-01 12:03:02 +03:00
|
|
|
|
if isinstance(exif, Image.Exif):
|
2019-03-31 00:09:01 +03:00
|
|
|
|
exif = exif.tobytes(8)
|
2019-02-23 05:30:38 +03:00
|
|
|
|
if exif.startswith(b"Exif\x00\x00"):
|
|
|
|
|
exif = exif[6:]
|
|
|
|
|
chunk(fp, b"eXIf", exif)
|
|
|
|
|
|
2019-12-01 06:50:21 +03:00
|
|
|
|
if save_all:
|
2022-09-23 13:06:08 +03:00
|
|
|
|
_write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
else:
|
|
|
|
|
ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2020-03-11 13:40:17 +03:00
|
|
|
|
if info:
|
|
|
|
|
for info_chunk in info.chunks:
|
|
|
|
|
cid, data = info_chunk[:2]
|
|
|
|
|
if cid[1:2].islower():
|
|
|
|
|
# Private chunk
|
|
|
|
|
after_idat = info_chunk[2:3]
|
|
|
|
|
if after_idat:
|
|
|
|
|
chunk(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
|
|
|
|
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
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
def getchunks(im, **params):
|
|
|
|
|
"""Return a list of PNG chunks representing this image."""
|
|
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
|
class collector:
|
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)
|
2018-01-05 18:53:50 +03:00
|
|
|
|
crc = o32(_crc32(data, _crc32(cid)))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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)
|
2019-12-01 06:50:21 +03:00
|
|
|
|
Image.register_save_all(PngImageFile.format, _save_all)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2018-12-18 21:33:34 +03:00
|
|
|
|
Image.register_extensions(PngImageFile.format, [".png", ".apng"])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
|
Image.register_mime(PngImageFile.format, "image/png")
|