PngImagePlugin.py 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # PNG support code
  6. #
  7. # See "PNG (Portable Network Graphics) Specification, version 1.0;
  8. # W3C Recommendation", 1996-10-01, Thomas Boutell (ed.).
  9. #
  10. # history:
  11. # 1996-05-06 fl Created (couldn't resist it)
  12. # 1996-12-14 fl Upgraded, added read and verify support (0.2)
  13. # 1996-12-15 fl Separate PNG stream parser
  14. # 1996-12-29 fl Added write support, added getchunks
  15. # 1996-12-30 fl Eliminated circular references in decoder (0.3)
  16. # 1998-07-12 fl Read/write 16-bit images as mode I (0.4)
  17. # 2001-02-08 fl Added transparency support (from Zircon) (0.5)
  18. # 2001-04-16 fl Don't close data source in "open" method (0.6)
  19. # 2004-02-24 fl Don't even pretend to support interlaced files (0.7)
  20. # 2004-08-31 fl Do basic sanity check on chunk identifiers (0.8)
  21. # 2004-09-20 fl Added PngInfo chunk container
  22. # 2004-12-18 fl Added DPI read support (based on code by Niki Spahiev)
  23. # 2008-08-13 fl Added tRNS support for RGB images
  24. # 2009-03-06 fl Support for preserving ICC profiles (by Florian Hoech)
  25. # 2009-03-08 fl Added zTXT support (from Lowell Alleman)
  26. # 2009-03-29 fl Read interlaced PNG files (from Conrado Porto Lopes Gouvua)
  27. #
  28. # Copyright (c) 1997-2009 by Secret Labs AB
  29. # Copyright (c) 1996 by Fredrik Lundh
  30. #
  31. # See the README file for information on usage and redistribution.
  32. #
  33. from __future__ import annotations
  34. import itertools
  35. import logging
  36. import re
  37. import struct
  38. import warnings
  39. import zlib
  40. from enum import IntEnum
  41. from fractions import Fraction
  42. from typing import IO, NamedTuple, cast
  43. from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
  44. from ._binary import i16be as i16
  45. from ._binary import i32be as i32
  46. from ._binary import o8
  47. from ._binary import o16be as o16
  48. from ._binary import o32be as o32
  49. from ._deprecate import deprecate
  50. from ._util import DeferredError
  51. TYPE_CHECKING = False
  52. if TYPE_CHECKING:
  53. from collections.abc import Callable
  54. from typing import Any, NoReturn
  55. from . import _imaging
  56. logger = logging.getLogger(__name__)
  57. is_cid = re.compile(rb"\w\w\w\w").match
  58. _MAGIC = b"\211PNG\r\n\032\n"
  59. _MODES = {
  60. # supported bits/color combinations, and corresponding modes/rawmodes
  61. # Grayscale
  62. (1, 0): ("1", "1"),
  63. (2, 0): ("L", "L;2"),
  64. (4, 0): ("L", "L;4"),
  65. (8, 0): ("L", "L"),
  66. (16, 0): ("I;16", "I;16B"),
  67. # Truecolour
  68. (8, 2): ("RGB", "RGB"),
  69. (16, 2): ("RGB", "RGB;16B"),
  70. # Indexed-colour
  71. (1, 3): ("P", "P;1"),
  72. (2, 3): ("P", "P;2"),
  73. (4, 3): ("P", "P;4"),
  74. (8, 3): ("P", "P"),
  75. # Grayscale with alpha
  76. (8, 4): ("LA", "LA"),
  77. (16, 4): ("RGBA", "LA;16B"), # LA;16B->LA not yet available
  78. # Truecolour with alpha
  79. (8, 6): ("RGBA", "RGBA"),
  80. (16, 6): ("RGBA", "RGBA;16B"),
  81. }
  82. _simple_palette = re.compile(b"^\xff*\x00\xff*$")
  83. MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
  84. """
  85. Maximum decompressed size for a iTXt or zTXt chunk.
  86. Eliminates decompression bombs where compressed chunks can expand 1000x.
  87. See :ref:`Text in PNG File Format<png-text>`.
  88. """
  89. MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
  90. """
  91. Set the maximum total text chunk size.
  92. See :ref:`Text in PNG File Format<png-text>`.
  93. """
  94. # APNG frame disposal modes
  95. class Disposal(IntEnum):
  96. OP_NONE = 0
  97. """
  98. No disposal is done on this frame before rendering the next frame.
  99. See :ref:`Saving APNG sequences<apng-saving>`.
  100. """
  101. OP_BACKGROUND = 1
  102. """
  103. This frame’s modified region is cleared to fully transparent black before rendering
  104. the next frame.
  105. See :ref:`Saving APNG sequences<apng-saving>`.
  106. """
  107. OP_PREVIOUS = 2
  108. """
  109. This frame’s modified region is reverted to the previous frame’s contents before
  110. rendering the next frame.
  111. See :ref:`Saving APNG sequences<apng-saving>`.
  112. """
  113. # APNG frame blend modes
  114. class Blend(IntEnum):
  115. OP_SOURCE = 0
  116. """
  117. All color components of this frame, including alpha, overwrite the previous output
  118. image contents.
  119. See :ref:`Saving APNG sequences<apng-saving>`.
  120. """
  121. OP_OVER = 1
  122. """
  123. This frame should be alpha composited with the previous output image contents.
  124. See :ref:`Saving APNG sequences<apng-saving>`.
  125. """
  126. def _safe_zlib_decompress(s: bytes) -> bytes:
  127. dobj = zlib.decompressobj()
  128. plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)
  129. if dobj.unconsumed_tail:
  130. msg = "Decompressed data too large for PngImagePlugin.MAX_TEXT_CHUNK"
  131. raise ValueError(msg)
  132. return plaintext
  133. def _crc32(data: bytes, seed: int = 0) -> int:
  134. return zlib.crc32(data, seed) & 0xFFFFFFFF
  135. # --------------------------------------------------------------------
  136. # Support classes. Suitable for PNG and related formats like MNG etc.
  137. class ChunkStream:
  138. def __init__(self, fp: IO[bytes]) -> None:
  139. self.fp: IO[bytes] | None = fp
  140. self.queue: list[tuple[bytes, int, int]] | None = []
  141. def read(self) -> tuple[bytes, int, int]:
  142. """Fetch a new chunk. Returns header information."""
  143. cid = None
  144. assert self.fp is not None
  145. if self.queue:
  146. cid, pos, length = self.queue.pop()
  147. self.fp.seek(pos)
  148. else:
  149. s = self.fp.read(8)
  150. cid = s[4:]
  151. pos = self.fp.tell()
  152. length = i32(s)
  153. if not is_cid(cid):
  154. if not ImageFile.LOAD_TRUNCATED_IMAGES:
  155. msg = f"broken PNG file (chunk {repr(cid)})"
  156. raise SyntaxError(msg)
  157. return cid, pos, length
  158. def __enter__(self) -> ChunkStream:
  159. return self
  160. def __exit__(self, *args: object) -> None:
  161. self.close()
  162. def close(self) -> None:
  163. self.queue = self.fp = None
  164. def push(self, cid: bytes, pos: int, length: int) -> None:
  165. assert self.queue is not None
  166. self.queue.append((cid, pos, length))
  167. def call(self, cid: bytes, pos: int, length: int) -> bytes:
  168. """Call the appropriate chunk handler"""
  169. logger.debug("STREAM %r %s %s", cid, pos, length)
  170. return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)
  171. def crc(self, cid: bytes, data: bytes) -> None:
  172. """Read and verify checksum"""
  173. # Skip CRC checks for ancillary chunks if allowed to load truncated
  174. # images
  175. # 5th byte of first char is 1 [specs, section 5.4]
  176. if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):
  177. self.crc_skip(cid, data)
  178. return
  179. assert self.fp is not None
  180. try:
  181. crc1 = _crc32(data, _crc32(cid))
  182. crc2 = i32(self.fp.read(4))
  183. if crc1 != crc2:
  184. msg = f"broken PNG file (bad header checksum in {repr(cid)})"
  185. raise SyntaxError(msg)
  186. except struct.error as e:
  187. msg = f"broken PNG file (incomplete checksum in {repr(cid)})"
  188. raise SyntaxError(msg) from e
  189. def crc_skip(self, cid: bytes, data: bytes) -> None:
  190. """Read checksum"""
  191. assert self.fp is not None
  192. self.fp.read(4)
  193. def verify(self, endchunk: bytes = b"IEND") -> list[bytes]:
  194. # Simple approach; just calculate checksum for all remaining
  195. # blocks. Must be called directly after open.
  196. cids = []
  197. assert self.fp is not None
  198. while True:
  199. try:
  200. cid, pos, length = self.read()
  201. except struct.error as e:
  202. msg = "truncated PNG file"
  203. raise OSError(msg) from e
  204. if cid == endchunk:
  205. break
  206. self.crc(cid, ImageFile._safe_read(self.fp, length))
  207. cids.append(cid)
  208. return cids
  209. class iTXt(str):
  210. """
  211. Subclass of string to allow iTXt chunks to look like strings while
  212. keeping their extra information
  213. """
  214. lang: str | bytes | None
  215. tkey: str | bytes | None
  216. @staticmethod
  217. def __new__(
  218. cls, text: str, lang: str | None = None, tkey: str | None = None
  219. ) -> iTXt:
  220. """
  221. :param cls: the class to use when creating the instance
  222. :param text: value for this key
  223. :param lang: language code
  224. :param tkey: UTF-8 version of the key name
  225. """
  226. self = str.__new__(cls, text)
  227. self.lang = lang
  228. self.tkey = tkey
  229. return self
  230. class PngInfo:
  231. """
  232. PNG chunk container (for use with save(pnginfo=))
  233. """
  234. def __init__(self) -> None:
  235. self.chunks: list[tuple[bytes, bytes, bool]] = []
  236. def add(self, cid: bytes, data: bytes, after_idat: bool = False) -> None:
  237. """Appends an arbitrary chunk. Use with caution.
  238. :param cid: a byte string, 4 bytes long.
  239. :param data: a byte string of the encoded data
  240. :param after_idat: for use with private chunks. Whether the chunk
  241. should be written after IDAT
  242. """
  243. self.chunks.append((cid, data, after_idat))
  244. def add_itxt(
  245. self,
  246. key: str | bytes,
  247. value: str | bytes,
  248. lang: str | bytes = "",
  249. tkey: str | bytes = "",
  250. zip: bool = False,
  251. ) -> None:
  252. """Appends an iTXt chunk.
  253. :param key: latin-1 encodable text key name
  254. :param value: value for this key
  255. :param lang: language code
  256. :param tkey: UTF-8 version of the key name
  257. :param zip: compression flag
  258. """
  259. if not isinstance(key, bytes):
  260. key = key.encode("latin-1", "strict")
  261. if not isinstance(value, bytes):
  262. value = value.encode("utf-8", "strict")
  263. if not isinstance(lang, bytes):
  264. lang = lang.encode("utf-8", "strict")
  265. if not isinstance(tkey, bytes):
  266. tkey = tkey.encode("utf-8", "strict")
  267. if zip:
  268. self.add(
  269. b"iTXt",
  270. key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" + zlib.compress(value),
  271. )
  272. else:
  273. self.add(b"iTXt", key + b"\0\0\0" + lang + b"\0" + tkey + b"\0" + value)
  274. def add_text(
  275. self, key: str | bytes, value: str | bytes | iTXt, zip: bool = False
  276. ) -> None:
  277. """Appends a text chunk.
  278. :param key: latin-1 encodable text key name
  279. :param value: value for this key, text or an
  280. :py:class:`PIL.PngImagePlugin.iTXt` instance
  281. :param zip: compression flag
  282. """
  283. if isinstance(value, iTXt):
  284. return self.add_itxt(
  285. key,
  286. value,
  287. value.lang if value.lang is not None else b"",
  288. value.tkey if value.tkey is not None else b"",
  289. zip=zip,
  290. )
  291. # The tEXt chunk stores latin-1 text
  292. if not isinstance(value, bytes):
  293. try:
  294. value = value.encode("latin-1", "strict")
  295. except UnicodeError:
  296. return self.add_itxt(key, value, zip=zip)
  297. if not isinstance(key, bytes):
  298. key = key.encode("latin-1", "strict")
  299. if zip:
  300. self.add(b"zTXt", key + b"\0\0" + zlib.compress(value))
  301. else:
  302. self.add(b"tEXt", key + b"\0" + value)
  303. # --------------------------------------------------------------------
  304. # PNG image stream (IHDR/IEND)
  305. class _RewindState(NamedTuple):
  306. info: dict[str | tuple[int, int], Any]
  307. tile: list[ImageFile._Tile]
  308. seq_num: int | None
  309. class PngStream(ChunkStream):
  310. def __init__(self, fp: IO[bytes]) -> None:
  311. super().__init__(fp)
  312. # local copies of Image attributes
  313. self.im_info: dict[str | tuple[int, int], Any] = {}
  314. self.im_text: dict[str, str | iTXt] = {}
  315. self.im_size = (0, 0)
  316. self.im_mode = ""
  317. self.im_tile: list[ImageFile._Tile] = []
  318. self.im_palette: tuple[str, bytes] | None = None
  319. self.im_custom_mimetype: str | None = None
  320. self.im_n_frames: int | None = None
  321. self._seq_num: int | None = None
  322. self.rewind_state = _RewindState({}, [], None)
  323. self.text_memory = 0
  324. def check_text_memory(self, chunklen: int) -> None:
  325. self.text_memory += chunklen
  326. if self.text_memory > MAX_TEXT_MEMORY:
  327. msg = (
  328. "Too much memory used in text chunks: "
  329. f"{self.text_memory}>MAX_TEXT_MEMORY"
  330. )
  331. raise ValueError(msg)
  332. def save_rewind(self) -> None:
  333. self.rewind_state = _RewindState(
  334. self.im_info.copy(),
  335. self.im_tile,
  336. self._seq_num,
  337. )
  338. def rewind(self) -> None:
  339. self.im_info = self.rewind_state.info.copy()
  340. self.im_tile = self.rewind_state.tile
  341. self._seq_num = self.rewind_state.seq_num
  342. def chunk_iCCP(self, pos: int, length: int) -> bytes:
  343. # ICC profile
  344. assert self.fp is not None
  345. s = ImageFile._safe_read(self.fp, length)
  346. # according to PNG spec, the iCCP chunk contains:
  347. # Profile name 1-79 bytes (character string)
  348. # Null separator 1 byte (null character)
  349. # Compression method 1 byte (0)
  350. # Compressed profile n bytes (zlib with deflate compression)
  351. i = s.find(b"\0")
  352. logger.debug("iCCP profile name %r", s[:i])
  353. comp_method = s[i + 1]
  354. logger.debug("Compression method %s", comp_method)
  355. if comp_method != 0:
  356. msg = f"Unknown compression method {comp_method} in iCCP chunk"
  357. raise SyntaxError(msg)
  358. try:
  359. icc_profile = _safe_zlib_decompress(s[i + 2 :])
  360. except ValueError:
  361. if ImageFile.LOAD_TRUNCATED_IMAGES:
  362. icc_profile = None
  363. else:
  364. raise
  365. except zlib.error:
  366. icc_profile = None # FIXME
  367. self.im_info["icc_profile"] = icc_profile
  368. return s
  369. def chunk_IHDR(self, pos: int, length: int) -> bytes:
  370. # image header
  371. assert self.fp is not None
  372. s = ImageFile._safe_read(self.fp, length)
  373. if length < 13:
  374. if ImageFile.LOAD_TRUNCATED_IMAGES:
  375. return s
  376. msg = "Truncated IHDR chunk"
  377. raise ValueError(msg)
  378. self.im_size = i32(s, 0), i32(s, 4)
  379. try:
  380. self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
  381. except Exception:
  382. pass
  383. if s[12]:
  384. self.im_info["interlace"] = 1
  385. if s[11]:
  386. msg = "unknown filter category"
  387. raise SyntaxError(msg)
  388. return s
  389. def chunk_IDAT(self, pos: int, length: int) -> NoReturn:
  390. # image data
  391. if "bbox" in self.im_info:
  392. tile = [ImageFile._Tile("zip", self.im_info["bbox"], pos, self.im_rawmode)]
  393. else:
  394. if self.im_n_frames is not None:
  395. self.im_info["default_image"] = True
  396. tile = [ImageFile._Tile("zip", (0, 0) + self.im_size, pos, self.im_rawmode)]
  397. self.im_tile = tile
  398. self.im_idat = length
  399. msg = "image data found"
  400. raise EOFError(msg)
  401. def chunk_IEND(self, pos: int, length: int) -> NoReturn:
  402. msg = "end of PNG image"
  403. raise EOFError(msg)
  404. def chunk_PLTE(self, pos: int, length: int) -> bytes:
  405. # palette
  406. assert self.fp is not None
  407. s = ImageFile._safe_read(self.fp, length)
  408. if self.im_mode == "P":
  409. self.im_palette = "RGB", s
  410. return s
  411. def chunk_tRNS(self, pos: int, length: int) -> bytes:
  412. # transparency
  413. assert self.fp is not None
  414. s = ImageFile._safe_read(self.fp, length)
  415. if self.im_mode == "P":
  416. if _simple_palette.match(s):
  417. # tRNS contains only one full-transparent entry,
  418. # other entries are full opaque
  419. i = s.find(b"\0")
  420. if i >= 0:
  421. self.im_info["transparency"] = i
  422. else:
  423. # otherwise, we have a byte string with one alpha value
  424. # for each palette entry
  425. self.im_info["transparency"] = s
  426. elif self.im_mode == "1":
  427. self.im_info["transparency"] = 255 if i16(s) else 0
  428. elif self.im_mode in ("L", "I;16"):
  429. self.im_info["transparency"] = i16(s)
  430. elif self.im_mode == "RGB":
  431. self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
  432. return s
  433. def chunk_gAMA(self, pos: int, length: int) -> bytes:
  434. # gamma setting
  435. assert self.fp is not None
  436. s = ImageFile._safe_read(self.fp, length)
  437. self.im_info["gamma"] = i32(s) / 100000.0
  438. return s
  439. def chunk_cHRM(self, pos: int, length: int) -> bytes:
  440. # chromaticity, 8 unsigned ints, actual value is scaled by 100,000
  441. # WP x,y, Red x,y, Green x,y Blue x,y
  442. assert self.fp is not None
  443. s = ImageFile._safe_read(self.fp, length)
  444. raw_vals = struct.unpack(f">{len(s) // 4}I", s)
  445. self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals)
  446. return s
  447. def chunk_sRGB(self, pos: int, length: int) -> bytes:
  448. # srgb rendering intent, 1 byte
  449. # 0 perceptual
  450. # 1 relative colorimetric
  451. # 2 saturation
  452. # 3 absolute colorimetric
  453. assert self.fp is not None
  454. s = ImageFile._safe_read(self.fp, length)
  455. if length < 1:
  456. if ImageFile.LOAD_TRUNCATED_IMAGES:
  457. return s
  458. msg = "Truncated sRGB chunk"
  459. raise ValueError(msg)
  460. self.im_info["srgb"] = s[0]
  461. return s
  462. def chunk_pHYs(self, pos: int, length: int) -> bytes:
  463. # pixels per unit
  464. assert self.fp is not None
  465. s = ImageFile._safe_read(self.fp, length)
  466. if length < 9:
  467. if ImageFile.LOAD_TRUNCATED_IMAGES:
  468. return s
  469. msg = "Truncated pHYs chunk"
  470. raise ValueError(msg)
  471. px, py = i32(s, 0), i32(s, 4)
  472. unit = s[8]
  473. if unit == 1: # meter
  474. dpi = px * 0.0254, py * 0.0254
  475. self.im_info["dpi"] = dpi
  476. elif unit == 0:
  477. self.im_info["aspect"] = px, py
  478. return s
  479. def chunk_tEXt(self, pos: int, length: int) -> bytes:
  480. # text
  481. assert self.fp is not None
  482. s = ImageFile._safe_read(self.fp, length)
  483. try:
  484. k, v = s.split(b"\0", 1)
  485. except ValueError:
  486. # fallback for broken tEXt tags
  487. k = s
  488. v = b""
  489. if k:
  490. k_str = k.decode("latin-1", "strict")
  491. v_str = v.decode("latin-1", "replace")
  492. self.im_info[k_str] = v if k == b"exif" else v_str
  493. self.im_text[k_str] = v_str
  494. self.check_text_memory(len(v_str))
  495. return s
  496. def chunk_zTXt(self, pos: int, length: int) -> bytes:
  497. # compressed text
  498. assert self.fp is not None
  499. s = ImageFile._safe_read(self.fp, length)
  500. try:
  501. k, v = s.split(b"\0", 1)
  502. except ValueError:
  503. k = s
  504. v = b""
  505. if v:
  506. comp_method = v[0]
  507. else:
  508. comp_method = 0
  509. if comp_method != 0:
  510. msg = f"Unknown compression method {comp_method} in zTXt chunk"
  511. raise SyntaxError(msg)
  512. try:
  513. v = _safe_zlib_decompress(v[1:])
  514. except ValueError:
  515. if ImageFile.LOAD_TRUNCATED_IMAGES:
  516. v = b""
  517. else:
  518. raise
  519. except zlib.error:
  520. v = b""
  521. if k:
  522. k_str = k.decode("latin-1", "strict")
  523. v_str = v.decode("latin-1", "replace")
  524. self.im_info[k_str] = self.im_text[k_str] = v_str
  525. self.check_text_memory(len(v_str))
  526. return s
  527. def chunk_iTXt(self, pos: int, length: int) -> bytes:
  528. # international text
  529. assert self.fp is not None
  530. r = s = ImageFile._safe_read(self.fp, length)
  531. try:
  532. k, r = r.split(b"\0", 1)
  533. except ValueError:
  534. return s
  535. if len(r) < 2:
  536. return s
  537. cf, cm, r = r[0], r[1], r[2:]
  538. try:
  539. lang, tk, v = r.split(b"\0", 2)
  540. except ValueError:
  541. return s
  542. if cf != 0:
  543. if cm == 0:
  544. try:
  545. v = _safe_zlib_decompress(v)
  546. except ValueError:
  547. if ImageFile.LOAD_TRUNCATED_IMAGES:
  548. return s
  549. else:
  550. raise
  551. except zlib.error:
  552. return s
  553. else:
  554. return s
  555. if k == b"XML:com.adobe.xmp":
  556. self.im_info["xmp"] = v
  557. try:
  558. k_str = k.decode("latin-1", "strict")
  559. lang_str = lang.decode("utf-8", "strict")
  560. tk_str = tk.decode("utf-8", "strict")
  561. v_str = v.decode("utf-8", "strict")
  562. except UnicodeError:
  563. return s
  564. self.im_info[k_str] = self.im_text[k_str] = iTXt(v_str, lang_str, tk_str)
  565. self.check_text_memory(len(v_str))
  566. return s
  567. def chunk_eXIf(self, pos: int, length: int) -> bytes:
  568. assert self.fp is not None
  569. s = ImageFile._safe_read(self.fp, length)
  570. self.im_info["exif"] = b"Exif\x00\x00" + s
  571. return s
  572. # APNG chunks
  573. def chunk_acTL(self, pos: int, length: int) -> bytes:
  574. assert self.fp is not None
  575. s = ImageFile._safe_read(self.fp, length)
  576. if length < 8:
  577. if ImageFile.LOAD_TRUNCATED_IMAGES:
  578. return s
  579. msg = "APNG contains truncated acTL chunk"
  580. raise ValueError(msg)
  581. if self.im_n_frames is not None:
  582. self.im_n_frames = None
  583. warnings.warn("Invalid APNG, will use default PNG image if possible")
  584. return s
  585. n_frames = i32(s)
  586. if n_frames == 0 or n_frames > 0x80000000:
  587. warnings.warn("Invalid APNG, will use default PNG image if possible")
  588. return s
  589. self.im_n_frames = n_frames
  590. self.im_info["loop"] = i32(s, 4)
  591. self.im_custom_mimetype = "image/apng"
  592. return s
  593. def chunk_fcTL(self, pos: int, length: int) -> bytes:
  594. assert self.fp is not None
  595. s = ImageFile._safe_read(self.fp, length)
  596. if length < 26:
  597. if ImageFile.LOAD_TRUNCATED_IMAGES:
  598. return s
  599. msg = "APNG contains truncated fcTL chunk"
  600. raise ValueError(msg)
  601. seq = i32(s)
  602. if (self._seq_num is None and seq != 0) or (
  603. self._seq_num is not None and self._seq_num != seq - 1
  604. ):
  605. msg = "APNG contains frame sequence errors"
  606. raise SyntaxError(msg)
  607. self._seq_num = seq
  608. width, height = i32(s, 4), i32(s, 8)
  609. px, py = i32(s, 12), i32(s, 16)
  610. im_w, im_h = self.im_size
  611. if px + width > im_w or py + height > im_h:
  612. msg = "APNG contains invalid frames"
  613. raise SyntaxError(msg)
  614. self.im_info["bbox"] = (px, py, px + width, py + height)
  615. delay_num, delay_den = i16(s, 20), i16(s, 22)
  616. if delay_den == 0:
  617. delay_den = 100
  618. self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000
  619. self.im_info["disposal"] = s[24]
  620. self.im_info["blend"] = s[25]
  621. return s
  622. def chunk_fdAT(self, pos: int, length: int) -> bytes:
  623. assert self.fp is not None
  624. if length < 4:
  625. if ImageFile.LOAD_TRUNCATED_IMAGES:
  626. s = ImageFile._safe_read(self.fp, length)
  627. return s
  628. msg = "APNG contains truncated fDAT chunk"
  629. raise ValueError(msg)
  630. s = ImageFile._safe_read(self.fp, 4)
  631. seq = i32(s)
  632. if self._seq_num != seq - 1:
  633. msg = "APNG contains frame sequence errors"
  634. raise SyntaxError(msg)
  635. self._seq_num = seq
  636. return self.chunk_IDAT(pos + 4, length - 4)
  637. # --------------------------------------------------------------------
  638. # PNG reader
  639. def _accept(prefix: bytes) -> bool:
  640. return prefix.startswith(_MAGIC)
  641. ##
  642. # Image plugin for PNG images.
  643. class PngImageFile(ImageFile.ImageFile):
  644. format = "PNG"
  645. format_description = "Portable network graphics"
  646. def _open(self) -> None:
  647. assert self.fp is not None
  648. if not _accept(self.fp.read(8)):
  649. msg = "not a PNG file"
  650. raise SyntaxError(msg)
  651. self._fp = self.fp
  652. self.__frame = 0
  653. #
  654. # Parse headers up to the first IDAT or fDAT chunk
  655. self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []
  656. self.png: PngStream | None = PngStream(self.fp)
  657. while True:
  658. #
  659. # get next chunk
  660. cid, pos, length = self.png.read()
  661. try:
  662. s = self.png.call(cid, pos, length)
  663. except EOFError:
  664. break
  665. except AttributeError:
  666. logger.debug("%r %s %s (unknown)", cid, pos, length)
  667. s = ImageFile._safe_read(self.fp, length)
  668. if cid[1:2].islower():
  669. self.private_chunks.append((cid, s))
  670. self.png.crc(cid, s)
  671. #
  672. # Copy relevant attributes from the PngStream. An alternative
  673. # would be to let the PngStream class modify these attributes
  674. # directly, but that introduces circular references which are
  675. # difficult to break if things go wrong in the decoder...
  676. # (believe me, I've tried ;-)
  677. self._mode = self.png.im_mode
  678. self._size = self.png.im_size
  679. self.info = self.png.im_info
  680. self._text: dict[str, str | iTXt] | None = None
  681. self.tile = self.png.im_tile
  682. self.custom_mimetype = self.png.im_custom_mimetype
  683. self.n_frames = self.png.im_n_frames or 1
  684. self.default_image = self.info.get("default_image", False)
  685. if self.png.im_palette:
  686. rawmode, data = self.png.im_palette
  687. self.palette = ImagePalette.raw(rawmode, data)
  688. if cid == b"fdAT":
  689. self.__prepare_idat = length - 4
  690. else:
  691. self.__prepare_idat = length # used by load_prepare()
  692. if self.png.im_n_frames is not None:
  693. self._close_exclusive_fp_after_loading = False
  694. self.png.save_rewind()
  695. self.__rewind_idat = self.__prepare_idat
  696. self.__rewind = self._fp.tell()
  697. if self.default_image:
  698. # IDAT chunk contains default image and not first animation frame
  699. self.n_frames += 1
  700. self._seek(0)
  701. self.is_animated = self.n_frames > 1
  702. @property
  703. def text(self) -> dict[str, str | iTXt]:
  704. # experimental
  705. if self._text is None:
  706. # iTxt, tEXt and zTXt chunks may appear at the end of the file
  707. # So load the file to ensure that they are read
  708. if self.is_animated:
  709. frame = self.__frame
  710. # for APNG, seek to the final frame before loading
  711. self.seek(self.n_frames - 1)
  712. self.load()
  713. if self.is_animated:
  714. self.seek(frame)
  715. assert self._text is not None
  716. return self._text
  717. def verify(self) -> None:
  718. """Verify PNG file"""
  719. if self.fp is None:
  720. msg = "verify must be called directly after open"
  721. raise RuntimeError(msg)
  722. # back up to beginning of IDAT block
  723. self.fp.seek(self.tile[0][2] - 8)
  724. assert self.png is not None
  725. self.png.verify()
  726. self.png.close()
  727. super().verify()
  728. def seek(self, frame: int) -> None:
  729. if not self._seek_check(frame):
  730. return
  731. if frame < self.__frame:
  732. self._seek(0, True)
  733. last_frame = self.__frame
  734. for f in range(self.__frame + 1, frame + 1):
  735. try:
  736. self._seek(f)
  737. except EOFError as e:
  738. self.seek(last_frame)
  739. msg = "no more images in APNG file"
  740. raise EOFError(msg) from e
  741. def _seek(self, frame: int, rewind: bool = False) -> None:
  742. assert self.png is not None
  743. if isinstance(self._fp, DeferredError):
  744. raise self._fp.ex
  745. self.dispose: _imaging.ImagingCore | None
  746. dispose_extent = None
  747. if frame == 0:
  748. if rewind:
  749. self._fp.seek(self.__rewind)
  750. self.png.rewind()
  751. self.__prepare_idat = self.__rewind_idat
  752. self._im = None
  753. self.info = self.png.im_info
  754. self.tile = self.png.im_tile
  755. self.fp = self._fp
  756. self._prev_im = None
  757. self.dispose = None
  758. self.default_image = self.info.get("default_image", False)
  759. self.dispose_op = self.info.get("disposal")
  760. self.blend_op = self.info.get("blend")
  761. dispose_extent = self.info.get("bbox")
  762. self.__frame = 0
  763. else:
  764. if frame != self.__frame + 1:
  765. msg = f"cannot seek to frame {frame}"
  766. raise ValueError(msg)
  767. # ensure previous frame was loaded
  768. self.load()
  769. if self.dispose:
  770. self.im.paste(self.dispose, self.dispose_extent)
  771. self._prev_im = self.im.copy()
  772. self.fp = self._fp
  773. # advance to the next frame
  774. if self.__prepare_idat:
  775. ImageFile._safe_read(self.fp, self.__prepare_idat)
  776. self.__prepare_idat = 0
  777. frame_start = False
  778. while True:
  779. self.fp.read(4) # CRC
  780. try:
  781. cid, pos, length = self.png.read()
  782. except (struct.error, SyntaxError):
  783. break
  784. if cid == b"IEND":
  785. msg = "No more images in APNG file"
  786. raise EOFError(msg)
  787. if cid == b"fcTL":
  788. if frame_start:
  789. # there must be at least one fdAT chunk between fcTL chunks
  790. msg = "APNG missing frame data"
  791. raise SyntaxError(msg)
  792. frame_start = True
  793. try:
  794. self.png.call(cid, pos, length)
  795. except UnicodeDecodeError:
  796. break
  797. except EOFError:
  798. if cid == b"fdAT":
  799. length -= 4
  800. if frame_start:
  801. self.__prepare_idat = length
  802. break
  803. ImageFile._safe_read(self.fp, length)
  804. except AttributeError:
  805. logger.debug("%r %s %s (unknown)", cid, pos, length)
  806. ImageFile._safe_read(self.fp, length)
  807. self.__frame = frame
  808. self.tile = self.png.im_tile
  809. self.dispose_op = self.info.get("disposal")
  810. self.blend_op = self.info.get("blend")
  811. dispose_extent = self.info.get("bbox")
  812. if not self.tile:
  813. msg = "image not found in APNG frame"
  814. raise EOFError(msg)
  815. if dispose_extent:
  816. self.dispose_extent: tuple[float, float, float, float] = dispose_extent
  817. # setup frame disposal (actual disposal done when needed in the next _seek())
  818. if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:
  819. self.dispose_op = Disposal.OP_BACKGROUND
  820. self.dispose = None
  821. if self.dispose_op == Disposal.OP_PREVIOUS:
  822. if self._prev_im:
  823. self.dispose = self._prev_im.copy()
  824. self.dispose = self._crop(self.dispose, self.dispose_extent)
  825. elif self.dispose_op == Disposal.OP_BACKGROUND:
  826. self.dispose = Image.core.fill(self.mode, self.size)
  827. self.dispose = self._crop(self.dispose, self.dispose_extent)
  828. def tell(self) -> int:
  829. return self.__frame
  830. def load_prepare(self) -> None:
  831. """internal: prepare to read PNG file"""
  832. if self.info.get("interlace"):
  833. self.decoderconfig = self.decoderconfig + (1,)
  834. self.__idat = self.__prepare_idat # used by load_read()
  835. ImageFile.ImageFile.load_prepare(self)
  836. def load_read(self, read_bytes: int) -> bytes:
  837. """internal: read more image data"""
  838. assert self.png is not None
  839. assert self.fp is not None
  840. while self.__idat == 0:
  841. # end of chunk, skip forward to next one
  842. self.fp.read(4) # CRC
  843. cid, pos, length = self.png.read()
  844. if cid not in [b"IDAT", b"DDAT", b"fdAT"]:
  845. self.png.push(cid, pos, length)
  846. return b""
  847. if cid == b"fdAT":
  848. try:
  849. self.png.call(cid, pos, length)
  850. except EOFError:
  851. pass
  852. self.__idat = length - 4 # sequence_num has already been read
  853. else:
  854. self.__idat = length # empty chunks are allowed
  855. # read more data from this chunk
  856. if read_bytes <= 0:
  857. read_bytes = self.__idat
  858. else:
  859. read_bytes = min(read_bytes, self.__idat)
  860. self.__idat = self.__idat - read_bytes
  861. return self.fp.read(read_bytes)
  862. def load_end(self) -> None:
  863. """internal: finished reading image data"""
  864. assert self.png is not None
  865. assert self.fp is not None
  866. if self.__idat != 0:
  867. self.fp.read(self.__idat)
  868. while True:
  869. self.fp.read(4) # CRC
  870. try:
  871. cid, pos, length = self.png.read()
  872. except (struct.error, SyntaxError):
  873. break
  874. if cid == b"IEND":
  875. break
  876. elif cid == b"fcTL" and self.is_animated:
  877. # start of the next frame, stop reading
  878. self.__prepare_idat = 0
  879. self.png.push(cid, pos, length)
  880. break
  881. try:
  882. self.png.call(cid, pos, length)
  883. except UnicodeDecodeError:
  884. break
  885. except EOFError:
  886. if cid == b"fdAT":
  887. length -= 4
  888. try:
  889. ImageFile._safe_read(self.fp, length)
  890. except OSError as e:
  891. if ImageFile.LOAD_TRUNCATED_IMAGES:
  892. break
  893. else:
  894. raise e
  895. except AttributeError:
  896. logger.debug("%r %s %s (unknown)", cid, pos, length)
  897. s = ImageFile._safe_read(self.fp, length)
  898. if cid[1:2].islower():
  899. self.private_chunks.append((cid, s, True))
  900. self._text = self.png.im_text
  901. if not self.is_animated:
  902. self.png.close()
  903. self.png = None
  904. else:
  905. if self._prev_im and self.blend_op == Blend.OP_OVER:
  906. updated = self._crop(self.im, self.dispose_extent)
  907. if self.im.mode == "RGB" and "transparency" in self.info:
  908. mask = updated.convert_transparent(
  909. "RGBA", self.info["transparency"]
  910. )
  911. else:
  912. if self.im.mode == "P" and "transparency" in self.info:
  913. t = self.info["transparency"]
  914. if isinstance(t, bytes):
  915. updated.putpalettealphas(t)
  916. elif isinstance(t, int):
  917. updated.putpalettealpha(t)
  918. mask = updated.convert("RGBA")
  919. self._prev_im.paste(updated, self.dispose_extent, mask)
  920. self.im = self._prev_im
  921. def _getexif(self) -> dict[int, Any] | None:
  922. if "exif" not in self.info:
  923. self.load()
  924. if "exif" not in self.info and "Raw profile type exif" not in self.info:
  925. return None
  926. return self.getexif()._get_merged_dict()
  927. def getexif(self) -> Image.Exif:
  928. if "exif" not in self.info:
  929. self.load()
  930. return super().getexif()
  931. # --------------------------------------------------------------------
  932. # PNG writer
  933. _OUTMODES = {
  934. # supported PIL modes, and corresponding rawmode, bit depth and color type
  935. "1": ("1", b"\x01", b"\x00"),
  936. "L;1": ("L;1", b"\x01", b"\x00"),
  937. "L;2": ("L;2", b"\x02", b"\x00"),
  938. "L;4": ("L;4", b"\x04", b"\x00"),
  939. "L": ("L", b"\x08", b"\x00"),
  940. "LA": ("LA", b"\x08", b"\x04"),
  941. "I": ("I;16B", b"\x10", b"\x00"),
  942. "I;16": ("I;16B", b"\x10", b"\x00"),
  943. "I;16B": ("I;16B", b"\x10", b"\x00"),
  944. "P;1": ("P;1", b"\x01", b"\x03"),
  945. "P;2": ("P;2", b"\x02", b"\x03"),
  946. "P;4": ("P;4", b"\x04", b"\x03"),
  947. "P": ("P", b"\x08", b"\x03"),
  948. "RGB": ("RGB", b"\x08", b"\x02"),
  949. "RGBA": ("RGBA", b"\x08", b"\x06"),
  950. }
  951. def putchunk(fp: IO[bytes], cid: bytes, *data: bytes) -> None:
  952. """Write a PNG chunk (including CRC field)"""
  953. byte_data = b"".join(data)
  954. fp.write(o32(len(byte_data)) + cid)
  955. fp.write(byte_data)
  956. crc = _crc32(byte_data, _crc32(cid))
  957. fp.write(o32(crc))
  958. class _idat:
  959. # wrap output from the encoder in IDAT chunks
  960. def __init__(self, fp: IO[bytes], chunk: Callable[..., None]) -> None:
  961. self.fp = fp
  962. self.chunk = chunk
  963. def write(self, data: bytes) -> None:
  964. self.chunk(self.fp, b"IDAT", data)
  965. class _fdat:
  966. # wrap encoder output in fdAT chunks
  967. def __init__(self, fp: IO[bytes], chunk: Callable[..., None], seq_num: int) -> None:
  968. self.fp = fp
  969. self.chunk = chunk
  970. self.seq_num = seq_num
  971. def write(self, data: bytes) -> None:
  972. self.chunk(self.fp, b"fdAT", o32(self.seq_num), data)
  973. self.seq_num += 1
  974. def _apply_encoderinfo(im: Image.Image, encoderinfo: dict[str, Any]) -> None:
  975. im.encoderconfig = (
  976. encoderinfo.get("optimize", False),
  977. encoderinfo.get("compress_level", -1),
  978. encoderinfo.get("compress_type", -1),
  979. encoderinfo.get("dictionary", b""),
  980. )
  981. class _Frame(NamedTuple):
  982. im: Image.Image
  983. bbox: tuple[int, int, int, int] | None
  984. encoderinfo: dict[str, Any]
  985. def _write_multiple_frames(
  986. im: Image.Image,
  987. fp: IO[bytes],
  988. chunk: Callable[..., None],
  989. mode: str,
  990. rawmode: str,
  991. default_image: Image.Image | None,
  992. append_images: list[Image.Image],
  993. ) -> Image.Image | None:
  994. duration = im.encoderinfo.get("duration")
  995. loop = im.encoderinfo.get("loop", im.info.get("loop", 0))
  996. disposal = im.encoderinfo.get("disposal", im.info.get("disposal", Disposal.OP_NONE))
  997. blend = im.encoderinfo.get("blend", im.info.get("blend", Blend.OP_SOURCE))
  998. if default_image:
  999. chain = itertools.chain(append_images)
  1000. else:
  1001. chain = itertools.chain([im], append_images)
  1002. im_frames: list[_Frame] = []
  1003. frame_count = 0
  1004. for im_seq in chain:
  1005. for im_frame in ImageSequence.Iterator(im_seq):
  1006. if im_frame.mode == mode:
  1007. im_frame = im_frame.copy()
  1008. else:
  1009. im_frame = im_frame.convert(mode)
  1010. encoderinfo = im.encoderinfo.copy()
  1011. if isinstance(duration, (list, tuple)):
  1012. encoderinfo["duration"] = duration[frame_count]
  1013. elif duration is None and "duration" in im_frame.info:
  1014. encoderinfo["duration"] = im_frame.info["duration"]
  1015. if isinstance(disposal, (list, tuple)):
  1016. encoderinfo["disposal"] = disposal[frame_count]
  1017. if isinstance(blend, (list, tuple)):
  1018. encoderinfo["blend"] = blend[frame_count]
  1019. frame_count += 1
  1020. if im_frames:
  1021. previous = im_frames[-1]
  1022. prev_disposal = previous.encoderinfo.get("disposal")
  1023. prev_blend = previous.encoderinfo.get("blend")
  1024. if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:
  1025. prev_disposal = Disposal.OP_BACKGROUND
  1026. if prev_disposal == Disposal.OP_BACKGROUND:
  1027. base_im = previous.im.copy()
  1028. dispose = Image.core.fill("RGBA", im.size, (0, 0, 0, 0))
  1029. bbox = previous.bbox
  1030. if bbox:
  1031. dispose = dispose.crop(bbox)
  1032. else:
  1033. bbox = (0, 0) + im.size
  1034. base_im.paste(dispose, bbox)
  1035. elif prev_disposal == Disposal.OP_PREVIOUS:
  1036. base_im = im_frames[-2].im
  1037. else:
  1038. base_im = previous.im
  1039. delta = ImageChops.subtract_modulo(
  1040. im_frame.convert("RGBA"), base_im.convert("RGBA")
  1041. )
  1042. bbox = delta.getbbox(alpha_only=False)
  1043. if (
  1044. not bbox
  1045. and prev_disposal == encoderinfo.get("disposal")
  1046. and prev_blend == encoderinfo.get("blend")
  1047. and "duration" in encoderinfo
  1048. ):
  1049. previous.encoderinfo["duration"] += encoderinfo["duration"]
  1050. continue
  1051. else:
  1052. bbox = None
  1053. im_frames.append(_Frame(im_frame, bbox, encoderinfo))
  1054. if len(im_frames) == 1 and not default_image:
  1055. return im_frames[0].im
  1056. # animation control
  1057. chunk(
  1058. fp,
  1059. b"acTL",
  1060. o32(len(im_frames)), # 0: num_frames
  1061. o32(loop), # 4: num_plays
  1062. )
  1063. # default image IDAT (if it exists)
  1064. if default_image:
  1065. default_im = im if im.mode == mode else im.convert(mode)
  1066. _apply_encoderinfo(default_im, im.encoderinfo)
  1067. ImageFile._save(
  1068. default_im,
  1069. cast(IO[bytes], _idat(fp, chunk)),
  1070. [ImageFile._Tile("zip", (0, 0) + im.size, 0, rawmode)],
  1071. )
  1072. seq_num = 0
  1073. for frame, frame_data in enumerate(im_frames):
  1074. im_frame = frame_data.im
  1075. if not frame_data.bbox:
  1076. bbox = (0, 0) + im_frame.size
  1077. else:
  1078. bbox = frame_data.bbox
  1079. im_frame = im_frame.crop(bbox)
  1080. size = im_frame.size
  1081. encoderinfo = frame_data.encoderinfo
  1082. frame_duration = encoderinfo.get("duration", 0)
  1083. delay = Fraction(frame_duration / 1000).limit_denominator(65535)
  1084. if delay.numerator > 65535:
  1085. msg = "cannot write duration"
  1086. raise ValueError(msg)
  1087. frame_disposal = encoderinfo.get("disposal", disposal)
  1088. frame_blend = encoderinfo.get("blend", blend)
  1089. # frame control
  1090. chunk(
  1091. fp,
  1092. b"fcTL",
  1093. o32(seq_num), # sequence_number
  1094. o32(size[0]), # width
  1095. o32(size[1]), # height
  1096. o32(bbox[0]), # x_offset
  1097. o32(bbox[1]), # y_offset
  1098. o16(delay.numerator), # delay_numerator
  1099. o16(delay.denominator), # delay_denominator
  1100. o8(frame_disposal), # dispose_op
  1101. o8(frame_blend), # blend_op
  1102. )
  1103. seq_num += 1
  1104. # frame data
  1105. _apply_encoderinfo(im_frame, im.encoderinfo)
  1106. if frame == 0 and not default_image:
  1107. # first frame must be in IDAT chunks for backwards compatibility
  1108. ImageFile._save(
  1109. im_frame,
  1110. cast(IO[bytes], _idat(fp, chunk)),
  1111. [ImageFile._Tile("zip", (0, 0) + im_frame.size, 0, rawmode)],
  1112. )
  1113. else:
  1114. fdat_chunks = _fdat(fp, chunk, seq_num)
  1115. ImageFile._save(
  1116. im_frame,
  1117. cast(IO[bytes], fdat_chunks),
  1118. [ImageFile._Tile("zip", (0, 0) + im_frame.size, 0, rawmode)],
  1119. )
  1120. seq_num = fdat_chunks.seq_num
  1121. return None
  1122. def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  1123. _save(im, fp, filename, save_all=True)
  1124. def _save(
  1125. im: Image.Image,
  1126. fp: IO[bytes],
  1127. filename: str | bytes,
  1128. chunk: Callable[..., None] = putchunk,
  1129. save_all: bool = False,
  1130. ) -> None:
  1131. # save an image to disk (called by the save method)
  1132. if save_all:
  1133. default_image = im.encoderinfo.get(
  1134. "default_image", im.info.get("default_image")
  1135. )
  1136. modes = set()
  1137. sizes = set()
  1138. append_images = im.encoderinfo.get("append_images", [])
  1139. for im_seq in itertools.chain([im], append_images):
  1140. for im_frame in ImageSequence.Iterator(im_seq):
  1141. modes.add(im_frame.mode)
  1142. sizes.add(im_frame.size)
  1143. for mode in ("RGBA", "RGB", "P"):
  1144. if mode in modes:
  1145. break
  1146. else:
  1147. mode = modes.pop()
  1148. size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))
  1149. else:
  1150. size = im.size
  1151. mode = im.mode
  1152. outmode = mode
  1153. if mode == "P":
  1154. #
  1155. # attempt to minimize storage requirements for palette images
  1156. if "bits" in im.encoderinfo:
  1157. # number of bits specified by user
  1158. colors = min(1 << im.encoderinfo["bits"], 256)
  1159. else:
  1160. # check palette contents
  1161. if im.palette:
  1162. colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)
  1163. else:
  1164. colors = 256
  1165. if colors <= 16:
  1166. if colors <= 2:
  1167. bits = 1
  1168. elif colors <= 4:
  1169. bits = 2
  1170. else:
  1171. bits = 4
  1172. outmode += f";{bits}"
  1173. # get the corresponding PNG mode
  1174. try:
  1175. rawmode, bit_depth, color_type = _OUTMODES[outmode]
  1176. except KeyError as e:
  1177. msg = f"cannot write mode {mode} as PNG"
  1178. raise OSError(msg) from e
  1179. if outmode == "I":
  1180. deprecate("Saving I mode images as PNG", 13, stacklevel=4)
  1181. #
  1182. # write minimal PNG file
  1183. fp.write(_MAGIC)
  1184. chunk(
  1185. fp,
  1186. b"IHDR",
  1187. o32(size[0]), # 0: size
  1188. o32(size[1]),
  1189. bit_depth,
  1190. color_type,
  1191. b"\0", # 10: compression
  1192. b"\0", # 11: filter category
  1193. b"\0", # 12: interlace flag
  1194. )
  1195. chunks = [b"cHRM", b"cICP", b"gAMA", b"sBIT", b"sRGB", b"tIME"]
  1196. icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
  1197. if icc:
  1198. # ICC profile
  1199. # according to PNG spec, the iCCP chunk contains:
  1200. # Profile name 1-79 bytes (character string)
  1201. # Null separator 1 byte (null character)
  1202. # Compression method 1 byte (0)
  1203. # Compressed profile n bytes (zlib with deflate compression)
  1204. name = b"ICC Profile"
  1205. data = name + b"\0\0" + zlib.compress(icc)
  1206. chunk(fp, b"iCCP", data)
  1207. # You must either have sRGB or iCCP.
  1208. # Disallow sRGB chunks when an iCCP-chunk has been emitted.
  1209. chunks.remove(b"sRGB")
  1210. info = im.encoderinfo.get("pnginfo")
  1211. if info:
  1212. chunks_multiple_allowed = [b"sPLT", b"iTXt", b"tEXt", b"zTXt"]
  1213. for info_chunk in info.chunks:
  1214. cid, data = info_chunk[:2]
  1215. if cid in chunks:
  1216. chunks.remove(cid)
  1217. chunk(fp, cid, data)
  1218. elif cid in chunks_multiple_allowed:
  1219. chunk(fp, cid, data)
  1220. elif cid[1:2].islower():
  1221. # Private chunk
  1222. after_idat = len(info_chunk) == 3 and info_chunk[2]
  1223. if not after_idat:
  1224. chunk(fp, cid, data)
  1225. if im.mode == "P":
  1226. palette_byte_number = colors * 3
  1227. palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
  1228. while len(palette_bytes) < palette_byte_number:
  1229. palette_bytes += b"\0"
  1230. chunk(fp, b"PLTE", palette_bytes)
  1231. transparency = im.encoderinfo.get("transparency", im.info.get("transparency", None))
  1232. if transparency or transparency == 0:
  1233. if im.mode == "P":
  1234. # limit to actual palette size
  1235. alpha_bytes = colors
  1236. if isinstance(transparency, bytes):
  1237. chunk(fp, b"tRNS", transparency[:alpha_bytes])
  1238. else:
  1239. transparency = max(0, min(255, transparency))
  1240. alpha = b"\xff" * transparency + b"\0"
  1241. chunk(fp, b"tRNS", alpha[:alpha_bytes])
  1242. elif im.mode in ("1", "L", "I", "I;16"):
  1243. transparency = max(0, min(65535, transparency))
  1244. chunk(fp, b"tRNS", o16(transparency))
  1245. elif im.mode == "RGB":
  1246. red, green, blue = transparency
  1247. chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue))
  1248. else:
  1249. if "transparency" in im.encoderinfo:
  1250. # don't bother with transparency if it's an RGBA
  1251. # and it's in the info dict. It's probably just stale.
  1252. msg = "cannot use transparency for this mode"
  1253. raise OSError(msg)
  1254. else:
  1255. if im.mode == "P" and im.im.getpalettemode() == "RGBA":
  1256. alpha = im.im.getpalette("RGBA", "A")
  1257. alpha_bytes = colors
  1258. chunk(fp, b"tRNS", alpha[:alpha_bytes])
  1259. dpi = im.encoderinfo.get("dpi")
  1260. if dpi:
  1261. chunk(
  1262. fp,
  1263. b"pHYs",
  1264. o32(int(dpi[0] / 0.0254 + 0.5)),
  1265. o32(int(dpi[1] / 0.0254 + 0.5)),
  1266. b"\x01",
  1267. )
  1268. if info:
  1269. chunks = [b"bKGD", b"hIST"]
  1270. for info_chunk in info.chunks:
  1271. cid, data = info_chunk[:2]
  1272. if cid in chunks:
  1273. chunks.remove(cid)
  1274. chunk(fp, cid, data)
  1275. exif = im.encoderinfo.get("exif")
  1276. if exif:
  1277. if isinstance(exif, Image.Exif):
  1278. exif = exif.tobytes(8)
  1279. if exif.startswith(b"Exif\x00\x00"):
  1280. exif = exif[6:]
  1281. chunk(fp, b"eXIf", exif)
  1282. single_im: Image.Image | None = im
  1283. if save_all:
  1284. single_im = _write_multiple_frames(
  1285. im, fp, chunk, mode, rawmode, default_image, append_images
  1286. )
  1287. if single_im:
  1288. _apply_encoderinfo(single_im, im.encoderinfo)
  1289. ImageFile._save(
  1290. single_im,
  1291. cast(IO[bytes], _idat(fp, chunk)),
  1292. [ImageFile._Tile("zip", (0, 0) + single_im.size, 0, rawmode)],
  1293. )
  1294. if info:
  1295. for info_chunk in info.chunks:
  1296. cid, data = info_chunk[:2]
  1297. if cid[1:2].islower():
  1298. # Private chunk
  1299. after_idat = len(info_chunk) == 3 and info_chunk[2]
  1300. if after_idat:
  1301. chunk(fp, cid, data)
  1302. chunk(fp, b"IEND", b"")
  1303. if hasattr(fp, "flush"):
  1304. fp.flush()
  1305. # --------------------------------------------------------------------
  1306. # PNG chunk converter
  1307. def getchunks(im: Image.Image, **params: Any) -> list[tuple[bytes, bytes, bytes]]:
  1308. """Return a list of PNG chunks representing this image."""
  1309. from io import BytesIO
  1310. chunks = []
  1311. def append(fp: IO[bytes], cid: bytes, *data: bytes) -> None:
  1312. byte_data = b"".join(data)
  1313. crc = o32(_crc32(byte_data, _crc32(cid)))
  1314. chunks.append((cid, byte_data, crc))
  1315. fp = BytesIO()
  1316. try:
  1317. im.encoderinfo = params
  1318. _save(im, fp, "", append)
  1319. finally:
  1320. del im.encoderinfo
  1321. return chunks
  1322. # --------------------------------------------------------------------
  1323. # Registry
  1324. Image.register_open(PngImageFile.format, PngImageFile, _accept)
  1325. Image.register_save(PngImageFile.format, _save)
  1326. Image.register_save_all(PngImageFile.format, _save_all)
  1327. Image.register_extensions(PngImageFile.format, [".png", ".apng"])
  1328. Image.register_mime(PngImageFile.format, "image/png")