PngImagePlugin.py 47 KB

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