features.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. from __future__ import annotations
  2. import collections
  3. import os
  4. import sys
  5. import warnings
  6. from typing import IO
  7. import PIL
  8. from . import Image
  9. from ._deprecate import deprecate
  10. modules = {
  11. "pil": ("PIL._imaging", "PILLOW_VERSION"),
  12. "tkinter": ("PIL._tkinter_finder", "tk_version"),
  13. "freetype2": ("PIL._imagingft", "freetype2_version"),
  14. "littlecms2": ("PIL._imagingcms", "littlecms_version"),
  15. "webp": ("PIL._webp", "webpdecoder_version"),
  16. }
  17. def check_module(feature: str) -> bool:
  18. """
  19. Checks if a module is available.
  20. :param feature: The module to check for.
  21. :returns: ``True`` if available, ``False`` otherwise.
  22. :raises ValueError: If the module is not defined in this version of Pillow.
  23. """
  24. if feature not in modules:
  25. msg = f"Unknown module {feature}"
  26. raise ValueError(msg)
  27. module, ver = modules[feature]
  28. try:
  29. __import__(module)
  30. return True
  31. except ModuleNotFoundError:
  32. return False
  33. except ImportError as ex:
  34. warnings.warn(str(ex))
  35. return False
  36. def version_module(feature: str) -> str | None:
  37. """
  38. :param feature: The module to check for.
  39. :returns:
  40. The loaded version number as a string, or ``None`` if unknown or not available.
  41. :raises ValueError: If the module is not defined in this version of Pillow.
  42. """
  43. if not check_module(feature):
  44. return None
  45. module, ver = modules[feature]
  46. return getattr(__import__(module, fromlist=[ver]), ver)
  47. def get_supported_modules() -> list[str]:
  48. """
  49. :returns: A list of all supported modules.
  50. """
  51. return [f for f in modules if check_module(f)]
  52. codecs = {
  53. "jpg": ("jpeg", "jpeglib"),
  54. "jpg_2000": ("jpeg2k", "jp2klib"),
  55. "zlib": ("zip", "zlib"),
  56. "libtiff": ("libtiff", "libtiff"),
  57. }
  58. def check_codec(feature: str) -> bool:
  59. """
  60. Checks if a codec is available.
  61. :param feature: The codec to check for.
  62. :returns: ``True`` if available, ``False`` otherwise.
  63. :raises ValueError: If the codec is not defined in this version of Pillow.
  64. """
  65. if feature not in codecs:
  66. msg = f"Unknown codec {feature}"
  67. raise ValueError(msg)
  68. codec, lib = codecs[feature]
  69. return f"{codec}_encoder" in dir(Image.core)
  70. def version_codec(feature: str) -> str | None:
  71. """
  72. :param feature: The codec to check for.
  73. :returns:
  74. The version number as a string, or ``None`` if not available.
  75. Checked at compile time for ``jpg``, run-time otherwise.
  76. :raises ValueError: If the codec is not defined in this version of Pillow.
  77. """
  78. if not check_codec(feature):
  79. return None
  80. codec, lib = codecs[feature]
  81. version = getattr(Image.core, f"{lib}_version")
  82. if feature == "libtiff":
  83. return version.split("\n")[0].split("Version ")[1]
  84. return version
  85. def get_supported_codecs() -> list[str]:
  86. """
  87. :returns: A list of all supported codecs.
  88. """
  89. return [f for f in codecs if check_codec(f)]
  90. features: dict[str, tuple[str, str | bool, str | None]] = {
  91. "webp_anim": ("PIL._webp", True, None),
  92. "webp_mux": ("PIL._webp", True, None),
  93. "transp_webp": ("PIL._webp", True, None),
  94. "raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"),
  95. "fribidi": ("PIL._imagingft", "HAVE_FRIBIDI", "fribidi_version"),
  96. "harfbuzz": ("PIL._imagingft", "HAVE_HARFBUZZ", "harfbuzz_version"),
  97. "libjpeg_turbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO", "libjpeg_turbo_version"),
  98. "zlib_ng": ("PIL._imaging", "HAVE_ZLIBNG", "zlib_ng_version"),
  99. "libimagequant": ("PIL._imaging", "HAVE_LIBIMAGEQUANT", "imagequant_version"),
  100. "xcb": ("PIL._imaging", "HAVE_XCB", None),
  101. }
  102. def check_feature(feature: str) -> bool | None:
  103. """
  104. Checks if a feature is available.
  105. :param feature: The feature to check for.
  106. :returns: ``True`` if available, ``False`` if unavailable, ``None`` if unknown.
  107. :raises ValueError: If the feature is not defined in this version of Pillow.
  108. """
  109. if feature not in features:
  110. msg = f"Unknown feature {feature}"
  111. raise ValueError(msg)
  112. module, flag, ver = features[feature]
  113. if isinstance(flag, bool):
  114. deprecate(f'check_feature("{feature}")', 12)
  115. try:
  116. imported_module = __import__(module, fromlist=["PIL"])
  117. if isinstance(flag, bool):
  118. return flag
  119. return getattr(imported_module, flag)
  120. except ModuleNotFoundError:
  121. return None
  122. except ImportError as ex:
  123. warnings.warn(str(ex))
  124. return None
  125. def version_feature(feature: str) -> str | None:
  126. """
  127. :param feature: The feature to check for.
  128. :returns: The version number as a string, or ``None`` if not available.
  129. :raises ValueError: If the feature is not defined in this version of Pillow.
  130. """
  131. if not check_feature(feature):
  132. return None
  133. module, flag, ver = features[feature]
  134. if ver is None:
  135. return None
  136. return getattr(__import__(module, fromlist=[ver]), ver)
  137. def get_supported_features() -> list[str]:
  138. """
  139. :returns: A list of all supported features.
  140. """
  141. supported_features = []
  142. for f, (module, flag, _) in features.items():
  143. if flag is True:
  144. for feature, (feature_module, _) in modules.items():
  145. if feature_module == module:
  146. if check_module(feature):
  147. supported_features.append(f)
  148. break
  149. elif check_feature(f):
  150. supported_features.append(f)
  151. return supported_features
  152. def check(feature: str) -> bool | None:
  153. """
  154. :param feature: A module, codec, or feature name.
  155. :returns:
  156. ``True`` if the module, codec, or feature is available,
  157. ``False`` or ``None`` otherwise.
  158. """
  159. if feature in modules:
  160. return check_module(feature)
  161. if feature in codecs:
  162. return check_codec(feature)
  163. if feature in features:
  164. return check_feature(feature)
  165. warnings.warn(f"Unknown feature '{feature}'.", stacklevel=2)
  166. return False
  167. def version(feature: str) -> str | None:
  168. """
  169. :param feature:
  170. The module, codec, or feature to check for.
  171. :returns:
  172. The version number as a string, or ``None`` if unknown or not available.
  173. """
  174. if feature in modules:
  175. return version_module(feature)
  176. if feature in codecs:
  177. return version_codec(feature)
  178. if feature in features:
  179. return version_feature(feature)
  180. return None
  181. def get_supported() -> list[str]:
  182. """
  183. :returns: A list of all supported modules, features, and codecs.
  184. """
  185. ret = get_supported_modules()
  186. ret.extend(get_supported_features())
  187. ret.extend(get_supported_codecs())
  188. return ret
  189. def pilinfo(out: IO[str] | None = None, supported_formats: bool = True) -> None:
  190. """
  191. Prints information about this installation of Pillow.
  192. This function can be called with ``python3 -m PIL``.
  193. It can also be called with ``python3 -m PIL.report`` or ``python3 -m PIL --report``
  194. to have "supported_formats" set to ``False``, omitting the list of all supported
  195. image file formats.
  196. :param out:
  197. The output stream to print to. Defaults to ``sys.stdout`` if ``None``.
  198. :param supported_formats:
  199. If ``True``, a list of all supported image file formats will be printed.
  200. """
  201. if out is None:
  202. out = sys.stdout
  203. Image.init()
  204. print("-" * 68, file=out)
  205. print(f"Pillow {PIL.__version__}", file=out)
  206. py_version_lines = sys.version.splitlines()
  207. print(f"Python {py_version_lines[0].strip()}", file=out)
  208. for py_version in py_version_lines[1:]:
  209. print(f" {py_version.strip()}", file=out)
  210. print("-" * 68, file=out)
  211. print(f"Python executable is {sys.executable or 'unknown'}", file=out)
  212. if sys.prefix != sys.base_prefix:
  213. print(f"Environment Python files loaded from {sys.prefix}", file=out)
  214. print(f"System Python files loaded from {sys.base_prefix}", file=out)
  215. print("-" * 68, file=out)
  216. print(
  217. f"Python Pillow modules loaded from {os.path.dirname(Image.__file__)}",
  218. file=out,
  219. )
  220. print(
  221. f"Binary Pillow modules loaded from {os.path.dirname(Image.core.__file__)}",
  222. file=out,
  223. )
  224. print("-" * 68, file=out)
  225. for name, feature in [
  226. ("pil", "PIL CORE"),
  227. ("tkinter", "TKINTER"),
  228. ("freetype2", "FREETYPE2"),
  229. ("littlecms2", "LITTLECMS2"),
  230. ("webp", "WEBP"),
  231. ("jpg", "JPEG"),
  232. ("jpg_2000", "OPENJPEG (JPEG2000)"),
  233. ("zlib", "ZLIB (PNG/ZIP)"),
  234. ("libtiff", "LIBTIFF"),
  235. ("raqm", "RAQM (Bidirectional Text)"),
  236. ("libimagequant", "LIBIMAGEQUANT (Quantization method)"),
  237. ("xcb", "XCB (X protocol)"),
  238. ]:
  239. if check(name):
  240. v: str | None = None
  241. if name == "jpg":
  242. libjpeg_turbo_version = version_feature("libjpeg_turbo")
  243. if libjpeg_turbo_version is not None:
  244. v = "libjpeg-turbo " + libjpeg_turbo_version
  245. if v is None:
  246. v = version(name)
  247. if v is not None:
  248. version_static = name in ("pil", "jpg")
  249. if name == "littlecms2":
  250. # this check is also in src/_imagingcms.c:setup_module()
  251. version_static = tuple(int(x) for x in v.split(".")) < (2, 7)
  252. t = "compiled for" if version_static else "loaded"
  253. if name == "zlib":
  254. zlib_ng_version = version_feature("zlib_ng")
  255. if zlib_ng_version is not None:
  256. v += ", compiled for zlib-ng " + zlib_ng_version
  257. elif name == "raqm":
  258. for f in ("fribidi", "harfbuzz"):
  259. v2 = version_feature(f)
  260. if v2 is not None:
  261. v += f", {f} {v2}"
  262. print("---", feature, "support ok,", t, v, file=out)
  263. else:
  264. print("---", feature, "support ok", file=out)
  265. else:
  266. print("***", feature, "support not installed", file=out)
  267. print("-" * 68, file=out)
  268. if supported_formats:
  269. extensions = collections.defaultdict(list)
  270. for ext, i in Image.EXTENSION.items():
  271. extensions[i].append(ext)
  272. for i in sorted(Image.ID):
  273. line = f"{i}"
  274. if i in Image.MIME:
  275. line = f"{line} {Image.MIME[i]}"
  276. print(line, file=out)
  277. if i in extensions:
  278. print(
  279. "Extensions: {}".format(", ".join(sorted(extensions[i]))), file=out
  280. )
  281. features = []
  282. if i in Image.OPEN:
  283. features.append("open")
  284. if i in Image.SAVE:
  285. features.append("save")
  286. if i in Image.SAVE_ALL:
  287. features.append("save_all")
  288. if i in Image.DECODERS:
  289. features.append("decode")
  290. if i in Image.ENCODERS:
  291. features.append("encode")
  292. print("Features: {}".format(", ".join(features)), file=out)
  293. print("-" * 68, file=out)