Skip to content

Commit d6be133

Browse files
authored
Merge pull request #4688 from nulano/featers-docs
2 parents 58c527d + d05a08a commit d6be133

6 files changed

Lines changed: 129 additions & 7 deletions

File tree

docs/reference/ImageGrab.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ or the clipboard to a PIL image memory.
99

1010
.. versionadded:: 1.1.3
1111

12-
.. py:function:: PIL.ImageGrab.grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None)
12+
.. py:function:: grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None)
1313
1414
Take a snapshot of the screen. The pixels inside the bounding box are
1515
returned as an "RGBA" on macOS, or an "RGB" image otherwise.
@@ -26,12 +26,15 @@ or the clipboard to a PIL image memory.
2626

2727
.. versionadded:: 6.2.0
2828

29-
:param xdisplay: X11 Display address. Pass ``None`` to grab the default system screen. Pass ``""`` to grab the default X11 screen on Windows or macOS.
29+
:param xdisplay:
30+
X11 Display address. Pass ``None`` to grab the default system screen. Pass ``""`` to grab the default X11 screen on Windows or macOS.
31+
32+
You can check X11 support using :py:func:`PIL.features.check_feature` with ``feature="xcb"``.
3033

3134
.. versionadded:: 7.1.0
3235
:return: An image
3336

34-
.. py:function:: PIL.ImageGrab.grabclipboard()
37+
.. py:function:: grabclipboard()
3538
3639
Take a snapshot of the clipboard image, if any. Only macOS and Windows are currently supported.
3740

docs/reference/features.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. py:module:: PIL.features
2+
.. py:currentmodule:: PIL.features
3+
4+
:py:mod:`features` Module
5+
==========================
6+
7+
The :py:mod:`PIL.features` module can be used to detect which Pillow features are available on your system.
8+
9+
.. autofunction:: PIL.features.pilinfo
10+
.. autofunction:: PIL.features.check
11+
.. autofunction:: PIL.features.get_supported
12+
13+
Modules
14+
-------
15+
16+
Support for the following modules can be checked:
17+
18+
* ``pil``: The Pillow core module, required for all functionality.
19+
* ``tkinter``: Tkinter support.
20+
* ``freetype2``: FreeType font support via :py:func:`PIL.ImageFont.truetype`.
21+
* ``littlecms2``: LittleCMS 2 support via :py:mod:`PIL.ImageCms`.
22+
* ``webp``: WebP image support.
23+
24+
.. autofunction:: PIL.features.check_module
25+
.. autofunction:: PIL.features.get_supported_modules
26+
27+
Codecs
28+
------
29+
30+
These are only checked during Pillow compilation.
31+
If the required library was uninstalled from the system, the ``pil`` core module may fail to load instead.
32+
33+
Support for the following codecs can be checked:
34+
35+
* ``jpg``: (compile time) Libjpeg support, required for JPEG based image formats.
36+
* ``jpg_2000``: (compile time) OpenJPEG support, required for JPEG 2000 image formats.
37+
* ``zlib``: (compile time) Zlib support, required for zlib compressed formats, such as PNG.
38+
* ``libtiff``: (compile time) LibTIFF support, required for TIFF based image formats.
39+
40+
.. autofunction:: PIL.features.check_codec
41+
.. autofunction:: PIL.features.get_supported_codecs
42+
43+
Features
44+
--------
45+
46+
Some of these are only checked during Pillow compilation.
47+
If the required library was uninstalled from the system, the relevant module may fail to load instead.
48+
49+
Support for the following features can be checked:
50+
51+
* ``libjpeg_turbo``: (compile time) Whether Pillow was compiled against the libjpeg-turbo version of libjpeg.
52+
* ``transp_webp``: Support for transparency in WebP images.
53+
* ``webp_mux``: (compile time) Support for EXIF data in WebP images.
54+
* ``webp_anim``: (compile time) Support for animated WebP images.
55+
* ``raqm``: Raqm library, required for ``ImageFont.LAYOUT_RAQM`` in :py:func:`PIL.ImageFont.truetype`.
56+
* ``libimagequant``: (compile time) ImageQuant quantization support in :py:func:`PIL.Image.Image.quantize`.
57+
* ``xcb``: (compile time) Support for X11 in :py:func:`PIL.ImageGrab.grab` via the XCB library.
58+
59+
.. autofunction:: PIL.features.check_feature
60+
.. autofunction:: PIL.features.get_supported_features

docs/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Reference
3030
PSDraw
3131
PixelAccess
3232
PyAccess
33+
features
3334
../PIL
3435
plugins
3536
internal_design

src/PIL/Image.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,12 @@ def quantize(self, colors=256, method=None, kmeans=0, palette=None, dither=1):
10511051
of colors.
10521052
10531053
:param colors: The desired number of colors, <= 256
1054-
:param method: 0 = median cut
1055-
1 = maximum coverage
1056-
2 = fast octree
1057-
3 = libimagequant
1054+
:param method: ``Image.MEDIANCUT=0`` (median cut),
1055+
``Image.MAXCOVERAGE=1`` (maximum coverage),
1056+
``Image.FASTOCTREE=2`` (fast octree),
1057+
``Image.LIBIMAGEQUANT=3`` (libimagequant; check support using
1058+
:py:func:`PIL.features.check_feature`
1059+
with ``feature="libimagequant"``).
10581060
:param kmeans: Integer
10591061
:param palette: Quantize to the palette of given
10601062
:py:class:`PIL.Image.Image`.

src/PIL/ImageFont.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
637637
encoding of any text provided in subsequent operations.
638638
:param layout_engine: Which layout engine to use, if available:
639639
`ImageFont.LAYOUT_BASIC` or `ImageFont.LAYOUT_RAQM`.
640+
641+
You can check support for Raqm layout using
642+
:py:func:`PIL.features.check_feature` with ``feature="raqm"``.
643+
644+
.. versionadded:: 4.2.0
640645
:return: A font object.
641646
:exception OSError: If the file could not be read.
642647
"""

src/PIL/features.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818

1919
def check_module(feature):
20+
"""
21+
Checks if a module is available.
22+
23+
:param feature: The module to check for.
24+
:returns: ``True`` if available, ``False`` otherwise.
25+
:raises ValueError: If the module is not defined in this version of Pillow.
26+
"""
2027
if not (feature in modules):
2128
raise ValueError("Unknown module %s" % feature)
2229

@@ -30,13 +37,23 @@ def check_module(feature):
3037

3138

3239
def get_supported_modules():
40+
"""
41+
:returns: A list of all supported modules.
42+
"""
3343
return [f for f in modules if check_module(f)]
3444

3545

3646
codecs = {"jpg": "jpeg", "jpg_2000": "jpeg2k", "zlib": "zip", "libtiff": "libtiff"}
3747

3848

3949
def check_codec(feature):
50+
"""
51+
Checks if a codec is available.
52+
53+
:param feature: The codec to check for.
54+
:returns: ``True`` if available, ``False`` otherwise.
55+
:raises ValueError: If the codec is not defined in this version of Pillow.
56+
"""
4057
if feature not in codecs:
4158
raise ValueError("Unknown codec %s" % feature)
4259

@@ -46,6 +63,9 @@ def check_codec(feature):
4663

4764

4865
def get_supported_codecs():
66+
"""
67+
:returns: A list of all supported codecs.
68+
"""
4969
return [f for f in codecs if check_codec(f)]
5070

5171

@@ -61,6 +81,13 @@ def get_supported_codecs():
6181

6282

6383
def check_feature(feature):
84+
"""
85+
Checks if a feature is available.
86+
87+
:param feature: The feature to check for.
88+
:returns: ``True`` if available, ``False`` if unavailable, ``None`` if unknown.
89+
:raises ValueError: If the feature is not defined in this version of Pillow.
90+
"""
6491
if feature not in features:
6592
raise ValueError("Unknown feature %s" % feature)
6693

@@ -74,10 +101,20 @@ def check_feature(feature):
74101

75102

76103
def get_supported_features():
104+
"""
105+
:returns: A list of all supported features.
106+
"""
77107
return [f for f in features if check_feature(f)]
78108

79109

80110
def check(feature):
111+
"""
112+
:param feature: A module, feature, or codec name.
113+
:returns:
114+
``True`` if the module, feature, or codec is available,
115+
``False`` or ``None`` otherwise.
116+
"""
117+
81118
if feature in modules:
82119
return check_module(feature)
83120
if feature in codecs:
@@ -89,13 +126,27 @@ def check(feature):
89126

90127

91128
def get_supported():
129+
"""
130+
:returns: A list of all supported modules, features, and codecs.
131+
"""
132+
92133
ret = get_supported_modules()
93134
ret.extend(get_supported_features())
94135
ret.extend(get_supported_codecs())
95136
return ret
96137

97138

98139
def pilinfo(out=None, supported_formats=True):
140+
"""
141+
Prints information about this installation of Pillow.
142+
This function can be called with ``python -m PIL``.
143+
144+
:param out:
145+
The output stream to print to. Defaults to ``sys.stdout`` if ``None``.
146+
:param supported_formats:
147+
If ``True``, a list of all supported image file formats will be printed.
148+
"""
149+
99150
if out is None:
100151
out = sys.stdout
101152

0 commit comments

Comments
 (0)