You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

4 years ago
  1. from . import Image
  2. modules = {
  3. "pil": "PIL._imaging",
  4. "tkinter": "PIL._tkinter_finder",
  5. "freetype2": "PIL._imagingft",
  6. "littlecms2": "PIL._imagingcms",
  7. "webp": "PIL._webp",
  8. }
  9. def check_module(feature):
  10. if not (feature in modules):
  11. raise ValueError("Unknown module %s" % feature)
  12. module = modules[feature]
  13. try:
  14. __import__(module)
  15. return True
  16. except ImportError:
  17. return False
  18. def get_supported_modules():
  19. return [f for f in modules if check_module(f)]
  20. codecs = {
  21. "jpg": "jpeg",
  22. "jpg_2000": "jpeg2k",
  23. "zlib": "zip",
  24. "libtiff": "libtiff"
  25. }
  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. }
  39. def check_feature(feature):
  40. if feature not in features:
  41. raise ValueError("Unknown feature %s" % feature)
  42. module, flag = features[feature]
  43. try:
  44. imported_module = __import__(module, fromlist=['PIL'])
  45. return getattr(imported_module, flag)
  46. except ImportError:
  47. return None
  48. def get_supported_features():
  49. return [f for f in features if check_feature(f)]
  50. def check(feature):
  51. return (feature in modules and check_module(feature) or
  52. feature in codecs and check_codec(feature) or
  53. feature in features and check_feature(feature))
  54. def get_supported():
  55. ret = get_supported_modules()
  56. ret.extend(get_supported_features())
  57. ret.extend(get_supported_codecs())
  58. return ret