mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 09:44:31 +03:00
Merge pull request #7049 from radarhere/numpy
This commit is contained in:
commit
93afedfa27
|
@ -3031,21 +3031,29 @@ def frombuffer(mode, size, data, decoder_name="raw", *args):
|
||||||
def fromarray(obj, mode=None):
|
def fromarray(obj, mode=None):
|
||||||
"""
|
"""
|
||||||
Creates an image memory from an object exporting the array interface
|
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
|
If ``obj`` is not contiguous, then the ``tobytes`` method is called
|
||||||
and :py:func:`~PIL.Image.frombuffer` is used.
|
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
|
from PIL import Image
|
||||||
import numpy as np
|
import numpy as np
|
||||||
im = Image.open("hopper.jpg")
|
im = Image.open("hopper.jpg")
|
||||||
a = np.asarray(im)
|
a = np.asarray(im)
|
||||||
|
|
||||||
Then this can be used to convert it to a Pillow image::
|
When converting Pillow images to arrays however, only pixel values are
|
||||||
|
transferred. This means that P and PA mode images will lose their palette.
|
||||||
im = Image.fromarray(a)
|
|
||||||
|
|
||||||
:param obj: Object with array interface
|
:param obj: Object with array interface
|
||||||
:param mode: Optional mode to use when reading ``obj``. Will be determined from
|
:param mode: Optional mode to use when reading ``obj``. Will be determined from
|
||||||
|
|
Loading…
Reference in New Issue
Block a user