PngImagePlugin.py 50 KB

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