2019-05-25 21:11:33 +03:00
|
|
|
import collections
|
|
|
|
import os
|
|
|
|
import sys
|
2020-02-18 01:22:15 +03:00
|
|
|
import warnings
|
2019-05-25 21:11:33 +03:00
|
|
|
|
|
|
|
import PIL
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image
|
2015-05-14 12:43:28 +03:00
|
|
|
|
|
|
|
modules = {
|
2020-06-14 06:35:43 +03:00
|
|
|
"pil": ("PIL._imaging", "PILLOW_VERSION"),
|
2019-10-12 16:29:10 +03:00
|
|
|
"tkinter": ("PIL._tkinter_finder", None),
|
2020-06-14 06:35:43 +03:00
|
|
|
"freetype2": ("PIL._imagingft", "freetype2_version"),
|
|
|
|
"littlecms2": ("PIL._imagingcms", "littlecms_version"),
|
|
|
|
"webp": ("PIL._webp", "webpdecoder_version"),
|
2015-05-14 12:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-05-14 12:43:28 +03:00
|
|
|
def check_module(feature):
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
Checks if a module is available.
|
|
|
|
|
|
|
|
:param feature: The module to check for.
|
2020-06-14 01:45:29 +03:00
|
|
|
:returns: ``True`` if available, ``False`` otherwise.
|
2020-06-13 05:01:38 +03:00
|
|
|
:raises ValueError: If the module is not defined in this version of Pillow.
|
|
|
|
"""
|
2017-05-12 00:01:58 +03:00
|
|
|
if not (feature in modules):
|
2020-07-16 12:43:29 +03:00
|
|
|
raise ValueError(f"Unknown module {feature}")
|
2015-05-14 13:57:01 +03:00
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
module, ver = modules[feature]
|
2015-05-14 12:43:28 +03:00
|
|
|
|
|
|
|
try:
|
2018-03-03 12:28:58 +03:00
|
|
|
__import__(module)
|
2015-05-14 12:43:28 +03:00
|
|
|
return True
|
2017-05-12 00:01:58 +03:00
|
|
|
except ImportError:
|
|
|
|
return False
|
2015-05-14 12:43:28 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
def version_module(feature):
|
2020-06-14 06:35:43 +03:00
|
|
|
"""
|
|
|
|
:param feature: The module to check for.
|
|
|
|
:returns:
|
|
|
|
The loaded version number as a string, or ``None`` if unknown or not available.
|
|
|
|
:raises ValueError: If the module is not defined in this version of Pillow.
|
|
|
|
"""
|
2019-10-12 16:29:10 +03:00
|
|
|
if not check_module(feature):
|
|
|
|
return None
|
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
module, ver = modules[feature]
|
2019-10-12 16:29:10 +03:00
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
if ver is None:
|
2019-10-12 16:29:10 +03:00
|
|
|
return None
|
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
return getattr(__import__(module, fromlist=[ver]), ver)
|
2019-10-12 16:29:10 +03:00
|
|
|
|
|
|
|
|
2015-05-14 12:43:28 +03:00
|
|
|
def get_supported_modules():
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
:returns: A list of all supported modules.
|
|
|
|
"""
|
2017-05-12 00:01:58 +03:00
|
|
|
return [f for f in modules if check_module(f)]
|
2015-05-14 12:43:28 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
codecs = {
|
|
|
|
"jpg": ("jpeg", "jpeglib"),
|
|
|
|
"jpg_2000": ("jpeg2k", "jp2klib"),
|
|
|
|
"zlib": ("zip", "zlib"),
|
|
|
|
"libtiff": ("libtiff", "libtiff"),
|
|
|
|
}
|
2015-05-14 12:43:28 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-05-14 12:43:28 +03:00
|
|
|
def check_codec(feature):
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
Checks if a codec is available.
|
|
|
|
|
|
|
|
:param feature: The codec to check for.
|
2020-06-14 01:45:29 +03:00
|
|
|
:returns: ``True`` if available, ``False`` otherwise.
|
2020-06-13 05:01:38 +03:00
|
|
|
:raises ValueError: If the codec is not defined in this version of Pillow.
|
|
|
|
"""
|
2015-05-14 13:57:01 +03:00
|
|
|
if feature not in codecs:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise ValueError(f"Unknown codec {feature}")
|
2015-05-14 13:57:01 +03:00
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
codec, lib = codecs[feature]
|
2015-05-14 13:57:01 +03:00
|
|
|
|
2015-05-14 12:43:28 +03:00
|
|
|
return codec + "_encoder" in dir(Image.core)
|
|
|
|
|
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
def version_codec(feature):
|
2020-06-14 06:35:43 +03:00
|
|
|
"""
|
|
|
|
:param feature: The codec to check for.
|
|
|
|
:returns:
|
|
|
|
The version number as a string, or ``None`` if not available.
|
|
|
|
Checked at compile time for ``jpg``, run-time otherwise.
|
|
|
|
:raises ValueError: If the codec is not defined in this version of Pillow.
|
|
|
|
"""
|
2019-10-12 16:29:10 +03:00
|
|
|
if not check_codec(feature):
|
|
|
|
return None
|
|
|
|
|
|
|
|
codec, lib = codecs[feature]
|
|
|
|
|
|
|
|
version = getattr(Image.core, lib + "_version")
|
|
|
|
|
|
|
|
if feature == "libtiff":
|
|
|
|
return version.split("\n")[0].split("Version ")[1]
|
|
|
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
2015-05-14 12:43:28 +03:00
|
|
|
def get_supported_codecs():
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
:returns: A list of all supported codecs.
|
|
|
|
"""
|
2017-05-12 00:01:58 +03:00
|
|
|
return [f for f in codecs if check_codec(f)]
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
features = {
|
2020-06-14 06:35:43 +03:00
|
|
|
"webp_anim": ("PIL._webp", "HAVE_WEBPANIM", None),
|
|
|
|
"webp_mux": ("PIL._webp", "HAVE_WEBPMUX", None),
|
|
|
|
"transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY", None),
|
|
|
|
"raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"),
|
|
|
|
"libjpeg_turbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO", "libjpeg_turbo_version"),
|
|
|
|
"libimagequant": ("PIL._imaging", "HAVE_LIBIMAGEQUANT", "imagequant_version"),
|
|
|
|
"xcb": ("PIL._imaging", "HAVE_XCB", None),
|
2017-05-12 00:01:58 +03:00
|
|
|
}
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
def check_feature(feature):
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
Checks if a feature is available.
|
|
|
|
|
|
|
|
:param feature: The feature to check for.
|
2020-06-14 01:45:29 +03:00
|
|
|
:returns: ``True`` if available, ``False`` if unavailable, ``None`` if unknown.
|
2020-06-13 05:01:38 +03:00
|
|
|
:raises ValueError: If the feature is not defined in this version of Pillow.
|
|
|
|
"""
|
2017-05-12 00:01:58 +03:00
|
|
|
if feature not in features:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise ValueError(f"Unknown feature {feature}")
|
2017-05-12 00:01:58 +03:00
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
module, flag, ver = features[feature]
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
try:
|
2019-03-21 16:28:20 +03:00
|
|
|
imported_module = __import__(module, fromlist=["PIL"])
|
2017-05-12 00:01:58 +03:00
|
|
|
return getattr(imported_module, flag)
|
|
|
|
except ImportError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
def version_feature(feature):
|
|
|
|
"""
|
|
|
|
:param feature: The feature to check for.
|
|
|
|
:returns: The version number as a string, or ``None`` if not available.
|
|
|
|
:raises ValueError: If the feature is not defined in this version of Pillow.
|
|
|
|
"""
|
|
|
|
if not check_feature(feature):
|
|
|
|
return None
|
|
|
|
|
|
|
|
module, flag, ver = features[feature]
|
|
|
|
|
|
|
|
if ver is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return getattr(__import__(module, fromlist=[ver]), ver)
|
|
|
|
|
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
def get_supported_features():
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
:returns: A list of all supported features.
|
|
|
|
"""
|
2017-05-12 00:01:58 +03:00
|
|
|
return [f for f in features if check_feature(f)]
|
|
|
|
|
|
|
|
|
|
|
|
def check(feature):
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
2020-06-14 06:35:43 +03:00
|
|
|
:param feature: A module, codec, or feature name.
|
2020-06-13 05:01:38 +03:00
|
|
|
:returns:
|
2020-06-14 06:35:43 +03:00
|
|
|
``True`` if the module, codec, or feature is available,
|
2020-06-14 01:45:29 +03:00
|
|
|
``False`` or ``None`` otherwise.
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
|
2020-02-18 01:22:15 +03:00
|
|
|
if feature in modules:
|
|
|
|
return check_module(feature)
|
|
|
|
if feature in codecs:
|
|
|
|
return check_codec(feature)
|
|
|
|
if feature in features:
|
|
|
|
return check_feature(feature)
|
2020-07-16 12:43:29 +03:00
|
|
|
warnings.warn(f"Unknown feature '{feature}'.", stacklevel=2)
|
2020-02-18 01:22:15 +03:00
|
|
|
return False
|
2017-05-12 00:01:58 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
def version(feature):
|
2020-06-14 06:35:43 +03:00
|
|
|
"""
|
|
|
|
:param feature:
|
|
|
|
The module, codec, or feature to check for.
|
|
|
|
:returns:
|
|
|
|
The version number as a string, or ``None`` if unknown or not available.
|
|
|
|
"""
|
2019-10-12 16:29:10 +03:00
|
|
|
if feature in modules:
|
|
|
|
return version_module(feature)
|
|
|
|
if feature in codecs:
|
|
|
|
return version_codec(feature)
|
2020-06-14 06:35:43 +03:00
|
|
|
if feature in features:
|
|
|
|
return version_feature(feature)
|
2019-10-12 16:29:10 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
def get_supported():
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
:returns: A list of all supported modules, features, and codecs.
|
|
|
|
"""
|
|
|
|
|
2017-05-12 00:01:58 +03:00
|
|
|
ret = get_supported_modules()
|
|
|
|
ret.extend(get_supported_features())
|
|
|
|
ret.extend(get_supported_codecs())
|
|
|
|
return ret
|
2019-05-25 21:11:33 +03:00
|
|
|
|
|
|
|
|
2019-10-12 17:01:18 +03:00
|
|
|
def pilinfo(out=None, supported_formats=True):
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
Prints information about this installation of Pillow.
|
|
|
|
This function can be called with ``python -m PIL``.
|
|
|
|
|
|
|
|
:param out:
|
2020-06-14 01:45:29 +03:00
|
|
|
The output stream to print to. Defaults to ``sys.stdout`` if ``None``.
|
2020-06-13 05:01:38 +03:00
|
|
|
:param supported_formats:
|
2020-06-14 01:45:29 +03:00
|
|
|
If ``True``, a list of all supported image file formats will be printed.
|
2020-06-13 05:01:38 +03:00
|
|
|
"""
|
|
|
|
|
2019-05-25 21:11:33 +03:00
|
|
|
if out is None:
|
|
|
|
out = sys.stdout
|
|
|
|
|
|
|
|
Image.init()
|
|
|
|
|
|
|
|
print("-" * 68, file=out)
|
2020-07-16 12:43:29 +03:00
|
|
|
print(f"Pillow {PIL.__version__}", file=out)
|
2019-10-12 17:01:18 +03:00
|
|
|
py_version = sys.version.splitlines()
|
2020-07-16 12:43:29 +03:00
|
|
|
print(f"Python {py_version[0].strip()}", file=out)
|
2019-10-12 17:01:18 +03:00
|
|
|
for py_version in py_version[1:]:
|
2020-07-16 12:43:29 +03:00
|
|
|
print(f" {py_version.strip()}", file=out)
|
2019-05-25 21:11:33 +03:00
|
|
|
print("-" * 68, file=out)
|
|
|
|
print(
|
2020-09-01 20:16:46 +03:00
|
|
|
f"Python modules loaded from {os.path.dirname(Image.__file__)}",
|
|
|
|
file=out,
|
2019-05-25 21:11:33 +03:00
|
|
|
)
|
|
|
|
print(
|
2020-09-01 20:16:46 +03:00
|
|
|
f"Binary modules loaded from {os.path.dirname(Image.core.__file__)}",
|
|
|
|
file=out,
|
2019-05-25 21:11:33 +03:00
|
|
|
)
|
|
|
|
print("-" * 68, file=out)
|
|
|
|
|
|
|
|
for name, feature in [
|
|
|
|
("pil", "PIL CORE"),
|
|
|
|
("tkinter", "TKINTER"),
|
|
|
|
("freetype2", "FREETYPE2"),
|
|
|
|
("littlecms2", "LITTLECMS2"),
|
|
|
|
("webp", "WEBP"),
|
|
|
|
("transp_webp", "WEBP Transparency"),
|
|
|
|
("webp_mux", "WEBPMUX"),
|
|
|
|
("webp_anim", "WEBP Animation"),
|
|
|
|
("jpg", "JPEG"),
|
|
|
|
("jpg_2000", "OPENJPEG (JPEG2000)"),
|
|
|
|
("zlib", "ZLIB (PNG/ZIP)"),
|
|
|
|
("libtiff", "LIBTIFF"),
|
|
|
|
("raqm", "RAQM (Bidirectional Text)"),
|
2019-10-12 17:01:18 +03:00
|
|
|
("libimagequant", "LIBIMAGEQUANT (Quantization method)"),
|
2019-09-19 19:57:59 +03:00
|
|
|
("xcb", "XCB (X protocol)"),
|
2019-05-25 21:11:33 +03:00
|
|
|
]:
|
|
|
|
if check(name):
|
2020-06-14 06:35:43 +03:00
|
|
|
if name == "jpg" and check_feature("libjpeg_turbo"):
|
|
|
|
v = "libjpeg-turbo " + version_feature("libjpeg_turbo")
|
|
|
|
else:
|
|
|
|
v = version(name)
|
2019-10-12 16:29:10 +03:00
|
|
|
if v is not None:
|
2020-06-14 06:35:43 +03:00
|
|
|
t = "compiled for" if name in ("pil", "jpg") else "loaded"
|
2020-06-21 20:07:10 +03:00
|
|
|
print("---", feature, "support ok,", t, v, file=out)
|
2019-10-12 16:29:10 +03:00
|
|
|
else:
|
2020-06-14 06:35:43 +03:00
|
|
|
print("---", feature, "support ok", file=out)
|
2019-05-25 21:11:33 +03:00
|
|
|
else:
|
|
|
|
print("***", feature, "support not installed", file=out)
|
|
|
|
print("-" * 68, file=out)
|
|
|
|
|
2019-10-12 17:01:18 +03:00
|
|
|
if supported_formats:
|
2019-09-27 01:09:04 +03:00
|
|
|
extensions = collections.defaultdict(list)
|
|
|
|
for ext, i in Image.EXTENSION.items():
|
|
|
|
extensions[i].append(ext)
|
|
|
|
|
|
|
|
for i in sorted(Image.ID):
|
2020-07-16 12:43:29 +03:00
|
|
|
line = f"{i}"
|
2019-09-27 01:09:04 +03:00
|
|
|
if i in Image.MIME:
|
2020-07-16 12:43:29 +03:00
|
|
|
line = f"{line} {Image.MIME[i]}"
|
2019-09-27 01:09:04 +03:00
|
|
|
print(line, file=out)
|
|
|
|
|
|
|
|
if i in extensions:
|
|
|
|
print(
|
|
|
|
"Extensions: {}".format(", ".join(sorted(extensions[i]))), file=out
|
|
|
|
)
|
|
|
|
|
|
|
|
features = []
|
|
|
|
if i in Image.OPEN:
|
|
|
|
features.append("open")
|
|
|
|
if i in Image.SAVE:
|
|
|
|
features.append("save")
|
|
|
|
if i in Image.SAVE_ALL:
|
|
|
|
features.append("save_all")
|
|
|
|
if i in Image.DECODERS:
|
|
|
|
features.append("decode")
|
|
|
|
if i in Image.ENCODERS:
|
|
|
|
features.append("encode")
|
|
|
|
|
|
|
|
print("Features: {}".format(", ".join(features)), file=out)
|
|
|
|
print("-" * 68, file=out)
|