PngImagePlugin.py 50 KB

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