-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
I'm trying to see if there is a way to go from a Pillow image to a numpy array without duplicating the data.
I have found some resources elsewhere, but none of them gave me an answer:
- Somebody else's question on SO, without an answer - https://stackoverflow.com/questions/56391994/pil-image-to-numpy-array-without-copy
- numpy docs, which basically says this is not possible ("No copy is performed if the input is already an ndarray with matching dtype and order"), although my reading indicates there is more to this (
memoryview, various buffer types, array interfaces and their version) - https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html - Another PIL issue that might be relevant - WIP Allow new-style buffer interfaces to be read/write #2980
- Numpy issue discussing interop with Pillow, from about this comment on - ENH: Add support for PEP 3118 buffers with offsets (non-contiguous) numpy/numpy#7467 (comment)
What did you do?
I took https://en.wikipedia.org/wiki/Barack_Obama#/media/File:President_Barack_Obama.jpg (any file should work) and this MWE
import psutil # pip install psutil
import numpy as np
from PIL import Image
process = psutil.Process()
print(f"Start - {psutil.Process().memory_info().rss / 1024 / 1024:.02f}")
pil_image = Image.open("President_Barack_Obama.jpg")
print(f"After open - {psutil.Process().memory_info().rss / 1024 / 1024:.02f}")
pil_image.load()
print(f"After load - {psutil.Process().memory_info().rss / 1024 / 1024:.02f}")
np_image = np.asarray(pil_image)
print(f"After np.asarray - {psutil.Process().memory_info().rss / 1024 / 1024:.02f}")and got
Start - 25.93
After open - 26.59
After load - 61.24
After np.asarray - 113.98
meaning both PIL and numpy allocate memory (~35 and ~50 mb respectively). I've also tried using np.array (somehow takes 80 mb instead of 50 mb) instead of np.asarray, specifying the dtype for the numpy arrays (uint8, int32 (one long per pixel)), to no avail.
What did you expect to happen?
I hoped that the some mechanism could be used to share the memory between PIL and numpy. For my purposes, a solution that would work for RGBA 8-bit images would be good enough, but a generic solution is welcome, I've stumbled upon this a couple of time already.
What actually happened?
Both numpy and PIL allocated memory
What are your OS, Python and Pillow versions?
- OS: MacOS 10.14.6 (Mojave)
- Python: Python 3.7.4 (default, Jul 9 2019, 18:13:23)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin - Pillow: 6.1.0
- Numpy: 1.17.2
Thanks!