Skip to content

Pillow to NumPy without copying data #4064

@HonzaCustomInk

Description

@HonzaCustomInk

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:

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions