Pillow/src/PIL/features.py

171 lines
4.5 KiB
Python
Raw Normal View History

Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
import collections
import os
import sys
import PIL
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
2019-03-21 16:28:20 +03:00
codecs = {"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 = {
2019-03-21 16:28:20 +03:00
"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"),
2018-12-13 11:19:45 +03:00
"libjpeg_turbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO"),
2019-09-27 01:09:04 +03:00
"libimagequant": ("PIL._imaging", "HAVE_LIBIMAGEQUANT"),
}
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:
2019-03-21 16:28:20 +03:00
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):
2019-03-21 16:28:20 +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
Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
def pilinfo(out=None, supported_formats=True):
Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
if out is None:
out = sys.stdout
Image.init()
print("-" * 68, file=out)
print("Pillow {}".format(PIL.__version__), file=out)
py_version = sys.version.splitlines()
print("Python {}".format(py_version[0].strip()), file=out)
for py_version in py_version[1:]:
print(" {}".format(py_version.strip()), file=out)
Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
print("-" * 68, file=out)
print(
"Python modules loaded from {}".format(os.path.dirname(Image.__file__)),
file=out,
)
print(
"Binary modules loaded from {}".format(os.path.dirname(Image.core.__file__)),
file=out,
)
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)"),
("libimagequant", "LIBIMAGEQUANT (Quantization method)"),
Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
]:
if check(name):
print("---", feature, "support ok", file=out)
else:
print("***", feature, "support not installed", file=out)
print("-" * 68, file=out)
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):
line = "{}".format(i)
if i in Image.MIME:
line = "{} {}".format(line, Image.MIME[i])
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)