Pillow/src/PIL/features.py

87 lines
1.9 KiB
Python
Raw Normal View History

from . import Image
modules = {
2015-05-14 13:57:01 +03:00
"pil": "PIL._imaging",
"tkinter": "PIL._tkinter_finder",
2015-05-14 13:57:01 +03:00
"freetype2": "PIL._imagingft",
"littlecms2": "PIL._imagingcms",
"webp": "PIL._webp",
}
2018-03-03 12:54:00 +03:00
def check_module(feature):
if not (feature in modules):
2015-05-14 13:57:01 +03:00
raise ValueError("Unknown module %s" % feature)
module = modules[feature]
try:
2018-03-03 12:28:58 +03:00
__import__(module)
return True
except ImportError:
return False
2018-03-03 12:54:00 +03:00
def get_supported_modules():
return [f for f in modules if check_module(f)]
2018-03-03 12:54:00 +03:00
codecs = {
2015-05-14 13:57:01 +03:00
"jpg": "jpeg",
"jpg_2000": "jpeg2k",
"zlib": "zip",
"libtiff": "libtiff"
}
2018-03-03 12:54:00 +03:00
def check_codec(feature):
2015-05-14 13:57:01 +03:00
if feature not in codecs:
raise ValueError("Unknown codec %s" % feature)
codec = codecs[feature]
2015-05-14 13:57:01 +03:00
return codec + "_encoder" in dir(Image.core)
def get_supported_codecs():
return [f for f in codecs if check_codec(f)]
2018-03-03 12:54:00 +03:00
features = {
"webp_anim": ("PIL._webp", 'HAVE_WEBPANIM'),
"webp_mux": ("PIL._webp", 'HAVE_WEBPMUX'),
"transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY"),
2018-12-12 13:25:05 +03:00
"raqm": ("PIL._imagingft", "HAVE_RAQM"),
"libjpegturbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO")
}
2018-03-03 12:54:00 +03:00
def check_feature(feature):
if feature not in features:
raise ValueError("Unknown feature %s" % feature)
module, flag = features[feature]
try:
imported_module = __import__(module, fromlist=['PIL'])
return getattr(imported_module, flag)
except ImportError:
return None
def get_supported_features():
return [f for f in features if check_feature(f)]
def check(feature):
2018-04-09 16:19:59 +03:00
return (feature in modules and check_module(feature) or
feature in codecs and check_codec(feature) or
feature in features and check_feature(feature))
2018-03-03 12:54:00 +03:00
def get_supported():
ret = get_supported_modules()
ret.extend(get_supported_features())
ret.extend(get_supported_codecs())
return ret