features.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from __future__ import print_function, unicode_literals
  2. import collections
  3. import os
  4. import sys
  5. import PIL
  6. from . import Image
  7. modules = {
  8. "pil": "PIL._imaging",
  9. "tkinter": "PIL._tkinter_finder",
  10. "freetype2": "PIL._imagingft",
  11. "littlecms2": "PIL._imagingcms",
  12. "webp": "PIL._webp",
  13. }
  14. def check_module(feature):
  15. if not (feature in modules):
  16. raise ValueError("Unknown module %s" % feature)
  17. module = modules[feature]
  18. try:
  19. __import__(module)
  20. return True
  21. except ImportError:
  22. return False
  23. def get_supported_modules():
  24. return [f for f in modules if check_module(f)]
  25. codecs = {"jpg": "jpeg", "jpg_2000": "jpeg2k", "zlib": "zip", "libtiff": "libtiff"}
  26. def check_codec(feature):
  27. if feature not in codecs:
  28. raise ValueError("Unknown codec %s" % feature)
  29. codec = codecs[feature]
  30. return codec + "_encoder" in dir(Image.core)
  31. def get_supported_codecs():
  32. return [f for f in codecs if check_codec(f)]
  33. features = {
  34. "webp_anim": ("PIL._webp", "HAVE_WEBPANIM"),
  35. "webp_mux": ("PIL._webp", "HAVE_WEBPMUX"),
  36. "transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY"),
  37. "raqm": ("PIL._imagingft", "HAVE_RAQM"),
  38. "libjpeg_turbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO"),
  39. }
  40. def check_feature(feature):
  41. if feature not in features:
  42. raise ValueError("Unknown feature %s" % feature)
  43. module, flag = features[feature]
  44. try:
  45. imported_module = __import__(module, fromlist=["PIL"])
  46. return getattr(imported_module, flag)
  47. except ImportError:
  48. return None
  49. def get_supported_features():
  50. return [f for f in features if check_feature(f)]
  51. def check(feature):
  52. return (
  53. feature in modules
  54. and check_module(feature)
  55. or feature in codecs
  56. and check_codec(feature)
  57. or feature in features
  58. and check_feature(feature)
  59. )
  60. def get_supported():
  61. ret = get_supported_modules()
  62. ret.extend(get_supported_features())
  63. ret.extend(get_supported_codecs())
  64. return ret
  65. def pilinfo(out=None):
  66. if out is None:
  67. out = sys.stdout
  68. Image.init()
  69. print("-" * 68, file=out)
  70. print("Pillow {}".format(PIL.__version__), file=out)
  71. print("-" * 68, file=out)
  72. print(
  73. "Python modules loaded from {}".format(os.path.dirname(Image.__file__)),
  74. file=out,
  75. )
  76. print(
  77. "Binary modules loaded from {}".format(sys.executable),
  78. file=out,
  79. )
  80. print("-" * 68, file=out)
  81. v = sys.version.splitlines()
  82. print("Python {}".format(v[0].strip()), file=out)
  83. for v in v[1:]:
  84. print(" {}".format(v.strip()), file=out)
  85. print("-" * 68, file=out)
  86. for name, feature in [
  87. ("pil", "PIL CORE"),
  88. ("tkinter", "TKINTER"),
  89. ("freetype2", "FREETYPE2"),
  90. ("littlecms2", "LITTLECMS2"),
  91. ("webp", "WEBP"),
  92. ("transp_webp", "WEBP Transparency"),
  93. ("webp_mux", "WEBPMUX"),
  94. ("webp_anim", "WEBP Animation"),
  95. ("jpg", "JPEG"),
  96. ("jpg_2000", "OPENJPEG (JPEG2000)"),
  97. ("zlib", "ZLIB (PNG/ZIP)"),
  98. ("libtiff", "LIBTIFF"),
  99. ("raqm", "RAQM (Bidirectional Text)"),
  100. ]:
  101. if check(name):
  102. print("---", feature, "support ok", file=out)
  103. else:
  104. print("***", feature, "support not installed", file=out)
  105. print("-" * 68, file=out)
  106. extensions = collections.defaultdict(list)
  107. for ext, i in Image.EXTENSION.items():
  108. extensions[i].append(ext)
  109. for i in sorted(Image.ID):
  110. line = "{}".format(i)
  111. if i in Image.MIME:
  112. line = "{} {}".format(line, Image.MIME[i])
  113. print(line, file=out)
  114. if i in extensions:
  115. print("Extensions: {}".format(", ".join(sorted(extensions[i]))), file=out)
  116. features = []
  117. if i in Image.OPEN:
  118. features.append("open")
  119. if i in Image.SAVE:
  120. features.append("save")
  121. if i in Image.SAVE_ALL:
  122. features.append("save_all")
  123. if i in Image.DECODERS:
  124. features.append("decode")
  125. if i in Image.ENCODERS:
  126. features.append("encode")
  127. print("Features: {}".format(", ".join(features)), file=out)
  128. print("-" * 68, file=out)