AvifImagePlugin.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. from __future__ import annotations
  2. import os
  3. from io import BytesIO
  4. from typing import IO
  5. from . import ExifTags, Image, ImageFile
  6. try:
  7. from . import _avif
  8. SUPPORTED = True
  9. except ImportError:
  10. SUPPORTED = False
  11. # Decoder options as module globals, until there is a way to pass parameters
  12. # to Image.open (see https://github.com/python-pillow/Pillow/issues/569)
  13. DECODE_CODEC_CHOICE = "auto"
  14. DEFAULT_MAX_THREADS = 0
  15. def get_codec_version(codec_name: str) -> str | None:
  16. versions = _avif.codec_versions()
  17. for version in versions.split(", "):
  18. if version.split(" [")[0] == codec_name:
  19. return version.split(":")[-1].split(" ")[0]
  20. return None
  21. def _accept(prefix: bytes) -> bool | str:
  22. if prefix[4:8] != b"ftyp":
  23. return False
  24. major_brand = prefix[8:12]
  25. if major_brand in (
  26. # coding brands
  27. b"avif",
  28. b"avis",
  29. # We accept files with AVIF container brands; we can't yet know if
  30. # the ftyp box has the correct compatible brands, but if it doesn't
  31. # then the plugin will raise a SyntaxError which Pillow will catch
  32. # before moving on to the next plugin that accepts the file.
  33. #
  34. # Also, because this file might not actually be an AVIF file, we
  35. # don't raise an error if AVIF support isn't properly compiled.
  36. b"mif1",
  37. b"msf1",
  38. ):
  39. if not SUPPORTED:
  40. return (
  41. "image file could not be identified because AVIF support not installed"
  42. )
  43. return True
  44. return False
  45. def _get_default_max_threads() -> int:
  46. if DEFAULT_MAX_THREADS:
  47. return DEFAULT_MAX_THREADS
  48. if hasattr(os, "sched_getaffinity"):
  49. return len(os.sched_getaffinity(0))
  50. else:
  51. return os.cpu_count() or 1
  52. class AvifImageFile(ImageFile.ImageFile):
  53. format = "AVIF"
  54. format_description = "AVIF image"
  55. __frame = -1
  56. def _open(self) -> None:
  57. if not SUPPORTED:
  58. msg = "image file could not be opened because AVIF support not installed"
  59. raise SyntaxError(msg)
  60. if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available(
  61. DECODE_CODEC_CHOICE
  62. ):
  63. msg = "Invalid opening codec"
  64. raise ValueError(msg)
  65. assert self.fp is not None
  66. self._decoder = _avif.AvifDecoder(
  67. self.fp.read(),
  68. DECODE_CODEC_CHOICE,
  69. _get_default_max_threads(),
  70. )
  71. # Get info from decoder
  72. self._size, self.n_frames, self._mode, icc, exif, exif_orientation, xmp = (
  73. self._decoder.get_info()
  74. )
  75. self.is_animated = self.n_frames > 1
  76. if icc:
  77. self.info["icc_profile"] = icc
  78. if xmp:
  79. self.info["xmp"] = xmp
  80. if exif_orientation != 1 or exif:
  81. exif_data = Image.Exif()
  82. if exif:
  83. exif_data.load(exif)
  84. original_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
  85. else:
  86. original_orientation = 1
  87. if exif_orientation != original_orientation:
  88. exif_data[ExifTags.Base.Orientation] = exif_orientation
  89. exif = exif_data.tobytes()
  90. if exif:
  91. self.info["exif"] = exif
  92. self.seek(0)
  93. def seek(self, frame: int) -> None:
  94. if not self._seek_check(frame):
  95. return
  96. # Set tile
  97. self.__frame = frame
  98. self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 0, self.mode)]
  99. def load(self) -> Image.core.PixelAccess | None:
  100. if self.tile:
  101. # We need to load the image data for this frame
  102. data, timescale, pts_in_timescales, duration_in_timescales = (
  103. self._decoder.get_frame(self.__frame)
  104. )
  105. self.info["timestamp"] = round(1000 * (pts_in_timescales / timescale))
  106. self.info["duration"] = round(1000 * (duration_in_timescales / timescale))
  107. if self.fp and self._exclusive_fp:
  108. self.fp.close()
  109. self.fp = BytesIO(data)
  110. return super().load()
  111. def load_seek(self, pos: int) -> None:
  112. pass
  113. def tell(self) -> int:
  114. return self.__frame
  115. def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  116. _save(im, fp, filename, save_all=True)
  117. def _save(
  118. im: Image.Image, fp: IO[bytes], filename: str | bytes, save_all: bool = False
  119. ) -> None:
  120. info = im.encoderinfo.copy()
  121. if save_all:
  122. append_images = list(info.get("append_images", []))
  123. else:
  124. append_images = []
  125. total = 0
  126. for ims in [im] + append_images:
  127. total += getattr(ims, "n_frames", 1)
  128. quality = info.get("quality", 75)
  129. if not isinstance(quality, int) or quality < 0 or quality > 100:
  130. msg = "Invalid quality setting"
  131. raise ValueError(msg)
  132. duration = info.get("duration", 0)
  133. subsampling = info.get("subsampling", "4:2:0")
  134. speed = info.get("speed", 6)
  135. max_threads = info.get("max_threads", _get_default_max_threads())
  136. codec = info.get("codec", "auto")
  137. if codec != "auto" and not _avif.encoder_codec_available(codec):
  138. msg = "Invalid saving codec"
  139. raise ValueError(msg)
  140. range_ = info.get("range", "full")
  141. tile_rows_log2 = info.get("tile_rows", 0)
  142. tile_cols_log2 = info.get("tile_cols", 0)
  143. alpha_premultiplied = bool(info.get("alpha_premultiplied", False))
  144. autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0))
  145. icc_profile = info.get("icc_profile", im.info.get("icc_profile"))
  146. exif_orientation = 1
  147. if exif := info.get("exif"):
  148. if isinstance(exif, Image.Exif):
  149. exif_data = exif
  150. else:
  151. exif_data = Image.Exif()
  152. exif_data.load(exif)
  153. if ExifTags.Base.Orientation in exif_data:
  154. exif_orientation = exif_data.pop(ExifTags.Base.Orientation)
  155. exif = exif_data.tobytes() if exif_data else b""
  156. elif isinstance(exif, Image.Exif):
  157. exif = exif_data.tobytes()
  158. xmp = info.get("xmp")
  159. if isinstance(xmp, str):
  160. xmp = xmp.encode("utf-8")
  161. advanced = info.get("advanced")
  162. if advanced is not None:
  163. if isinstance(advanced, dict):
  164. advanced = advanced.items()
  165. try:
  166. advanced = tuple(advanced)
  167. except TypeError:
  168. invalid = True
  169. else:
  170. invalid = any(not isinstance(v, tuple) or len(v) != 2 for v in advanced)
  171. if invalid:
  172. msg = (
  173. "advanced codec options must be a dict of key-value string "
  174. "pairs or a series of key-value two-tuples"
  175. )
  176. raise ValueError(msg)
  177. # Setup the AVIF encoder
  178. enc = _avif.AvifEncoder(
  179. im.size,
  180. subsampling,
  181. quality,
  182. speed,
  183. max_threads,
  184. codec,
  185. range_,
  186. tile_rows_log2,
  187. tile_cols_log2,
  188. alpha_premultiplied,
  189. autotiling,
  190. icc_profile or b"",
  191. exif or b"",
  192. exif_orientation,
  193. xmp or b"",
  194. advanced,
  195. )
  196. # Add each frame
  197. frame_idx = 0
  198. frame_duration = 0
  199. cur_idx = im.tell()
  200. is_single_frame = total == 1
  201. try:
  202. for ims in [im] + append_images:
  203. # Get number of frames in this image
  204. nfr = getattr(ims, "n_frames", 1)
  205. for idx in range(nfr):
  206. ims.seek(idx)
  207. # Make sure image mode is supported
  208. frame = ims
  209. rawmode = ims.mode
  210. if ims.mode not in {"RGB", "RGBA"}:
  211. rawmode = "RGBA" if ims.has_transparency_data else "RGB"
  212. frame = ims.convert(rawmode)
  213. # Update frame duration
  214. if isinstance(duration, (list, tuple)):
  215. frame_duration = duration[frame_idx]
  216. else:
  217. frame_duration = duration
  218. # Append the frame to the animation encoder
  219. enc.add(
  220. frame.tobytes("raw", rawmode),
  221. frame_duration,
  222. frame.size,
  223. rawmode,
  224. is_single_frame,
  225. )
  226. # Update frame index
  227. frame_idx += 1
  228. if not save_all:
  229. break
  230. finally:
  231. im.seek(cur_idx)
  232. # Get the final output from the encoder
  233. data = enc.finish()
  234. if data is None:
  235. msg = "cannot write file as AVIF (encoder returned None)"
  236. raise OSError(msg)
  237. fp.write(data)
  238. Image.register_open(AvifImageFile.format, AvifImageFile, _accept)
  239. if SUPPORTED:
  240. Image.register_save(AvifImageFile.format, _save)
  241. Image.register_save_all(AvifImageFile.format, _save_all)
  242. Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"])
  243. Image.register_mime(AvifImageFile.format, "image/avif")