Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved init(), preinit() and register_mime() documentation #7399

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/reference/Image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ Generating images
Registering plugins
^^^^^^^^^^^^^^^^^^^

.. autofunction:: preinit
.. autofunction:: init

.. note::

These functions are for use by plugin authors. Application authors can
ignore them.
These functions are for use by plugin authors. They are called when a
plugin is loaded as part of :py:meth:`~preinit()` or :py:meth:`~init()`.
Application authors can ignore them.

.. autofunction:: register_open
.. autofunction:: register_mime
Expand Down
22 changes: 14 additions & 8 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ def getmodebands(mode):


def preinit():
"""Explicitly load standard file format drivers."""
"""
Explicitly loads BMP, GIF, JPEG, PPM and PPM file format drivers.

It is called when opening or saving images.
"""

global _initialized
if _initialized >= 1:
Expand Down Expand Up @@ -334,11 +338,6 @@ def preinit():
assert PngImagePlugin
except ImportError:
pass
# try:
# import TiffImagePlugin
# assert TiffImagePlugin
# except ImportError:
# pass

_initialized = 1

Expand All @@ -347,6 +346,9 @@ def init():
"""
Explicitly initializes the Python Imaging Library. This function
loads all available file format drivers.

It is called when opening or saving images if :py:meth:`~preinit()` is
insufficient, and by :py:meth:`~PIL.features.pilinfo`.
"""

global _initialized
Expand Down Expand Up @@ -3407,8 +3409,12 @@ def register_open(id, factory, accept=None):

def register_mime(id, mimetype):
"""
Registers an image MIME type. This function should not be used
in application code.
Registers an image MIME type by populating ``Image.MIME``. This function
should not be used in application code.

``Image.MIME`` provides a mapping from image format identifiers to mime
formats, but :py:meth:`~PIL.ImageFile.ImageFile.get_format_mimetype` can
provide a different result for specific images.

:param id: An image format identifier.
:param mimetype: The image MIME type for this format.
Expand Down