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

Document handling of modes when converting to or from NumPy #7049

Merged
merged 2 commits into from
Mar 31, 2023
Merged
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
18 changes: 13 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3031,21 +3031,29 @@ def frombuffer(mode, size, data, decoder_name="raw", *args):
def fromarray(obj, mode=None):
"""
Creates an image memory from an object exporting the array interface
(using the buffer protocol).
(using the buffer protocol)::

from PIL import Image
import numpy as np
a = np.zeros((5, 5))
im = Image.fromarray(a)

If ``obj`` is not contiguous, then the ``tobytes`` method is called
and :py:func:`~PIL.Image.frombuffer` is used.

If you have an image in NumPy::
In the case of NumPy, be aware that Pillow modes do not always correspond
to NumPy dtypes. Pillow modes only offer 1-bit pixels, 8-bit pixels,
32-signed integer pixels and 32-bit floating point pixels.

Pillow images can also be converted to arrays::

from PIL import Image
import numpy as np
im = Image.open("hopper.jpg")
a = np.asarray(im)

Then this can be used to convert it to a Pillow image::

im = Image.fromarray(a)
When converting Pillow images to arrays however, only pixel values are
transferred. This means that P and PA mode images will lose their palette.

:param obj: Object with array interface
:param mode: Optional mode to use when reading ``obj``. Will be determined from
Expand Down