Source code for PIL.PngImagePlugin

#
# 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.
#

import itertools
import logging
import re
import struct
import warnings
import zlib
from enum import IntEnum

from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
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
from ._deprecate import deprecate

logger = logging.getLogger(__name__)

is_cid = re.compile(rb"\w\w\w\w").match


_MAGIC = b"\211PNG\r\n\032\n"


_MODES = {
    # supported bits/color combinations, and corresponding modes/rawmodes
    # Greyscale
    (1, 0): ("1", "1"),
    (2, 0): ("L", "L;2"),
    (4, 0): ("L", "L;4"),
    (8, 0): ("L", "L"),
    (16, 0): ("I", "I;16B"),
    # Truecolour
    (8, 2): ("RGB", "RGB"),
    (16, 2): ("RGB", "RGB;16B"),
    # Indexed-colour
    (1, 3): ("P", "P;1"),
    (2, 3): ("P", "P;2"),
    (4, 3): ("P", "P;4"),
    (8, 3): ("P", "P"),
    # Greyscale with alpha
    (8, 4): ("LA", "LA"),
    (16, 4): ("RGBA", "LA;16B"),  # LA;16B->LA not yet available
    # Truecolour with alpha
    (8, 6): ("RGBA", "RGBA"),
    (16, 6): ("RGBA", "RGBA;16B"),
}


_simple_palette = re.compile(b"^\xff*\x00\xff*$")

MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
"""
Maximum decompressed size for a iTXt or zTXt chunk.
Eliminates decompression bombs where compressed chunks can expand 1000x.
See :ref:`Text in PNG File Format<png-text>`.
"""
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
"""
Set the maximum total text chunk size.
See :ref:`Text in PNG File Format<png-text>`.
"""


# APNG frame disposal modes
[docs]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>`. """
# APNG frame blend modes
[docs]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>`. """
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__: deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}") return enum[name] msg = f"module '{__name__}' has no attribute '{name}'" raise AttributeError(msg) def _safe_zlib_decompress(s): dobj = zlib.decompressobj() plaintext = dobj.decompress(s, MAX_TEXT_CHUNK) if dobj.unconsumed_tail: msg = "Decompressed Data Too Large" raise ValueError(msg) return plaintext def _crc32(data, seed=0): return zlib.crc32(data, seed) & 0xFFFFFFFF # -------------------------------------------------------------------- # Support classes. Suitable for PNG and related formats like MNG etc.
[docs]class ChunkStream: def __init__(self, fp): self.fp = fp self.queue = []
[docs] def read(self): """Fetch a new chunk. Returns header information.""" cid = None if self.queue: cid, pos, length = self.queue.pop() self.fp.seek(pos) else: s = self.fp.read(8) cid = s[4:] pos = self.fp.tell() length = i32(s) if not is_cid(cid): if not ImageFile.LOAD_TRUNCATED_IMAGES: msg = f"broken PNG file (chunk {repr(cid)})" raise SyntaxError(msg) return cid, pos, length
def __enter__(self): return self def __exit__(self, *args): self.close()
[docs] def close(self): self.queue = self.fp = None
[docs] def push(self, cid, pos, length): self.queue.append((cid, pos, length))
[docs] def call(self, cid, pos, length): """Call the appropriate chunk handler""" logger.debug("STREAM %r %s %s", cid, pos, length) return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
[docs] def crc(self, cid, data): """Read and verify checksum""" # Skip CRC checks for ancillary chunks if allowed to load truncated # images # 5th byte of first char is 1 [specs, section 5.4] if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1): self.crc_skip(cid, data) return try: crc1 = _crc32(data, _crc32(cid)) crc2 = i32(self.fp.read(4)) if crc1 != crc2: msg = f"broken PNG file (bad header checksum in {repr(cid)})" raise SyntaxError(msg) except struct.error as e: msg = f"broken PNG file (incomplete checksum in {repr(cid)})" raise SyntaxError(msg) from e
[docs] def crc_skip(self, cid, data): """Read checksum""" self.fp.read(4)
[docs] def verify(self, endchunk=b"IEND"): # Simple approach; just calculate checksum for all remaining # blocks. Must be called directly after open. cids = [] while True: try: cid, pos, length = self.read() except struct.error as e: msg = "truncated PNG file" raise OSError(msg) from e if cid == endchunk: break self.crc(cid, ImageFile._safe_read(self.fp, length)) cids.append(cid) return cids
[docs]class iTXt(str): """ Subclass of string to allow iTXt chunks to look like strings while keeping their extra information """
[docs] @staticmethod def __new__(cls, text, lang=None, tkey=None): """ :param cls: the class to use when creating the instance :param text: value for this key :param lang: language code :param tkey: UTF-8 version of the key name """ self = str.__new__(cls, text) self.lang = lang self.tkey = tkey return self
[docs]class PngInfo: """ PNG chunk container (for use with save(pnginfo=)) """ def __init__(self): self.chunks = []
[docs] def add(self, cid, data, after_idat=False): """Appends an arbitrary chunk. Use with caution. :param cid: a byte string, 4 bytes long. :param data: a byte string of the encoded data :param after_idat: for use with private chunks. Whether the chunk should be written after IDAT """ chunk = [cid, data] if after_idat: chunk.append(True) self.chunks.append(tuple(chunk))
[docs] def add_itxt(self, key, value, lang="", tkey="", zip=False): """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 """ 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: self.add( b"iTXt", key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" + zlib.compress(value), ) else: self.add(b"iTXt", key + b"\0\0\0" + lang + b"\0" + tkey + b"\0" + value)
[docs] def add_text(self, key, value, zip=False): """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 """ if isinstance(value, iTXt): return self.add_itxt(key, value, value.lang, value.tkey, zip=zip) # The tEXt chunk stores latin-1 text if not isinstance(value, bytes): try: value = value.encode("latin-1", "strict") except UnicodeError: return self.add_itxt(key, value, zip=zip) if not isinstance(key, bytes): key = key.encode("latin-1", "strict") if zip: self.add(b"zTXt", key + b"\0\0" + zlib.compress(value)) else: self.add(b"tEXt", key + b"\0" + value)
# -------------------------------------------------------------------- # PNG image stream (IHDR/IEND)
[docs]class PngStream(ChunkStream): def __init__(self, fp): super().__init__(fp) # local copies of Image attributes self.im_info = {} self.im_text = {} self.im_size = (0, 0) self.im_mode = None self.im_tile = None self.im_palette = None self.im_custom_mimetype = None self.im_n_frames = None self._seq_num = None self.rewind_state = None self.text_memory = 0
[docs] def check_text_memory(self, chunklen): self.text_memory += chunklen if self.text_memory > MAX_TEXT_MEMORY: msg = ( "Too much memory used in text chunks: " f"{self.text_memory}>MAX_TEXT_MEMORY" ) raise ValueError(msg)
[docs] def save_rewind(self): self.rewind_state = { "info": self.im_info.copy(), "tile": self.im_tile, "seq_num": self._seq_num, }
[docs] 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"]
[docs] def chunk_iCCP(self, pos, length): # ICC profile s = ImageFile._safe_read(self.fp, length) # 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) i = s.find(b"\0") logger.debug("iCCP profile name %r", s[:i]) logger.debug("Compression method %s", s[i]) comp_method = s[i] if comp_method != 0: msg = f"Unknown compression method {comp_method} in iCCP chunk" raise SyntaxError(msg) try: icc_profile = _safe_zlib_decompress(s[i + 2 :]) except ValueError: if ImageFile.LOAD_TRUNCATED_IMAGES: icc_profile = None else: raise except zlib.error: icc_profile = None # FIXME self.im_info["icc_profile"] = icc_profile return s
[docs] def chunk_IHDR(self, pos, length): # image header s = ImageFile._safe_read(self.fp, length) if length < 13: if ImageFile.LOAD_TRUNCATED_IMAGES: return s msg = "Truncated IHDR chunk" raise ValueError(msg) self.im_size = i32(s, 0), i32(s, 4) try: self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])] except Exception: pass if s[12]: self.im_info["interlace"] = 1 if s[11]: msg = "unknown filter category" raise SyntaxError(msg) return s
[docs] def chunk_IDAT(self, pos, length): # image data if "bbox" in self.im_info: tile = [("zip", self.im_info["bbox"], pos, self.im_rawmode)] 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 self.im_idat = length raise EOFError
[docs] def chunk_IEND(self, pos, length): # end of PNG image raise EOFError
[docs] def chunk_PLTE(self, pos, length): # palette s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": self.im_palette = "RGB", s return s
[docs] def chunk_tRNS(self, pos, length): # transparency s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": if _simple_palette.match(s): # tRNS contains only one full-transparent entry, # other entries are full opaque i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i else: # otherwise, we have a byte string with one alpha value # for each palette entry self.im_info["transparency"] = s elif self.im_mode in ("1", "L", "I"): 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
[docs] def chunk_gAMA(self, pos, length): # gamma setting s = ImageFile._safe_read(self.fp, length) self.im_info["gamma"] = i32(s) / 100000.0 return s
[docs] def chunk_cHRM(self, pos, length): # chromaticity, 8 unsigned ints, actual value is scaled by 100,000 # WP x,y, Red x,y, Green x,y Blue x,y s = ImageFile._safe_read(self.fp, length) raw_vals = struct.unpack(">%dI" % (len(s) // 4), s) self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals) return s
[docs] def chunk_sRGB(self, pos, length): # srgb rendering intent, 1 byte # 0 perceptual # 1 relative colorimetric # 2 saturation # 3 absolute colorimetric s = ImageFile._safe_read(self.fp, length) if length < 1: if ImageFile.LOAD_TRUNCATED_IMAGES: return s msg = "Truncated sRGB chunk" raise ValueError(msg) self.im_info["srgb"] = s[0] return s
[docs] def chunk_pHYs(self, pos, length): # pixels per unit s = ImageFile._safe_read(self.fp, length) if length < 9: if ImageFile.LOAD_TRUNCATED_IMAGES: return s msg = "Truncated pHYs chunk" raise ValueError(msg) px, py = i32(s, 0), i32(s, 4) unit = s[8] if unit == 1: # meter dpi = px * 0.0254, py * 0.0254 self.im_info["dpi"] = dpi elif unit == 0: self.im_info["aspect"] = px, py return s
[docs] def chunk_tEXt(self, pos, length): # text s = ImageFile._safe_read(self.fp, length) try: k, v = s.split(b"\0", 1) except ValueError: # fallback for broken tEXt tags k = s v = b"" if k: k = k.decode("latin-1", "strict") v_str = v.decode("latin-1", "replace") self.im_info[k] = v if k == "exif" else v_str self.im_text[k] = v_str self.check_text_memory(len(v_str)) return s
[docs] def chunk_zTXt(self, pos, length): # compressed text s = ImageFile._safe_read(self.fp, length) try: k, v = s.split(b"\0", 1) except ValueError: k = s v = b"" if v: comp_method = v[0] else: comp_method = 0 if comp_method != 0: msg = f"Unknown compression method {comp_method} in zTXt chunk" raise SyntaxError(msg) try: v = _safe_zlib_decompress(v[1:]) except ValueError: if ImageFile.LOAD_TRUNCATED_IMAGES: v = b"" else: raise except zlib.error: v = b"" if k: k = k.decode("latin-1", "strict") v = v.decode("latin-1", "replace") self.im_info[k] = self.im_text[k] = v self.check_text_memory(len(v)) return s
[docs] 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 = r[0], r[1], r[2:] try: lang, tk, v = r.split(b"\0", 2) except ValueError: return s if cf != 0: if cm == 0: try: v = _safe_zlib_decompress(v) except ValueError: if ImageFile.LOAD_TRUNCATED_IMAGES: return s else: raise except zlib.error: return s else: return s 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) self.check_text_memory(len(v)) return s
[docs] def chunk_eXIf(self, pos, length): s = ImageFile._safe_read(self.fp, length) self.im_info["exif"] = b"Exif\x00\x00" + s return s
# APNG chunks
[docs] def chunk_acTL(self, pos, length): s = ImageFile._safe_read(self.fp, length) if length < 8: if ImageFile.LOAD_TRUNCATED_IMAGES: return s msg = "APNG contains truncated acTL chunk" raise ValueError(msg) 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 self.im_info["loop"] = i32(s, 4) self.im_custom_mimetype = "image/apng" return s
[docs] def chunk_fcTL(self, pos, length): s = ImageFile._safe_read(self.fp, length) if length < 26: if ImageFile.LOAD_TRUNCATED_IMAGES: return s msg = "APNG contains truncated fcTL chunk" raise ValueError(msg) seq = i32(s) if (self._seq_num is None and seq != 0) or ( self._seq_num is not None and self._seq_num != seq - 1 ): msg = "APNG contains frame sequence errors" raise SyntaxError(msg) self._seq_num = seq width, height = i32(s, 4), i32(s, 8) px, py = i32(s, 12), i32(s, 16) im_w, im_h = self.im_size if px + width > im_w or py + height > im_h: msg = "APNG contains invalid frames" raise SyntaxError(msg) self.im_info["bbox"] = (px, py, px + width, py + height) delay_num, delay_den = i16(s, 20), i16(s, 22) if delay_den == 0: delay_den = 100 self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000 self.im_info["disposal"] = s[24] self.im_info["blend"] = s[25] return s
[docs] def chunk_fdAT(self, pos, length): if length < 4: if ImageFile.LOAD_TRUNCATED_IMAGES: s = ImageFile._safe_read(self.fp, length) return s msg = "APNG contains truncated fDAT chunk" raise ValueError(msg) s = ImageFile._safe_read(self.fp, 4) seq = i32(s) if self._seq_num != seq - 1: msg = "APNG contains frame sequence errors" raise SyntaxError(msg) self._seq_num = seq return self.chunk_IDAT(pos + 4, length - 4)
# -------------------------------------------------------------------- # PNG reader def _accept(prefix): return prefix[:8] == _MAGIC ## # Image plugin for PNG images.
[docs]class PngImageFile(ImageFile.ImageFile): format = "PNG" format_description = "Portable network graphics" def _open(self): if not _accept(self.fp.read(8)): msg = "not a PNG file" raise SyntaxError(msg) self._fp = self.fp self.__frame = 0 # # Parse headers up to the first IDAT or fDAT chunk self.private_chunks = [] self.png = PngStream(self.fp) while True: # # get next chunk cid, pos, length = self.png.read() try: s = self.png.call(cid, pos, length) except EOFError: break except AttributeError: logger.debug("%r %s %s (unknown)", cid, pos, length) s = ImageFile._safe_read(self.fp, length) if cid[1:2].islower(): self.private_chunks.append((cid, s)) 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 self._text = None self.tile = self.png.im_tile self.custom_mimetype = self.png.im_custom_mimetype self.n_frames = self.png.im_n_frames or 1 self.default_image = self.info.get("default_image", False) if self.png.im_palette: rawmode, data = self.png.im_palette self.palette = ImagePalette.raw(rawmode, data) if cid == b"fdAT": self.__prepare_idat = length - 4 else: self.__prepare_idat = length # used by load_prepare() if self.png.im_n_frames is not None: self._close_exclusive_fp_after_loading = False self.png.save_rewind() self.__rewind_idat = self.__prepare_idat self.__rewind = self._fp.tell() if self.default_image: # IDAT chunk contains default image and not first animation frame self.n_frames += 1 self._seek(0) self.is_animated = self.n_frames > 1 @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 if self.is_animated: frame = self.__frame # for APNG, seek to the final frame before loading self.seek(self.n_frames - 1) self.load() if self.is_animated: self.seek(frame) return self.tode, box
[docs] defss="p">, py return s) return self.tode, box
size, (as e: msg = length = self.read() # Greyscale (1, 0): >not hasatt if self.im_mode ,