Skip to content

np.asarray(PIL.Image) returns a read-only copy as of 9.2.0 #6581

@FirefoxMetzger

Description

@FirefoxMetzger

I'm not sure if this is a bug or intended behavior, but as of v9.2.0 a snippet like the following produces an array (baz) which is read-only:

>>> from PIL import Image
>>> import numpy as np
>>> foo = np.ones((255, 255, 3), dtype=np.uint8)
>>> Image.fromarray(foo).save("tmp.png") 
>>> bar = Image.open("tmp.png")
>>> baz = np.asarray(bar)
>>> baz.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In v9.1.0 the same snippet produces a writable array:

>>> from PIL import Image          
>>> import numpy as np
>>> foo = np.ones((255, 255, 3), dtype=np.uint8)
>>> Image.fromarray(foo).save("tmp.png")
>>> bar = Image.open("tmp.png")
>>> baz = np.asarray(bar)
>>> baz.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

I was wondering if this change was intentional, i.e., if we should rely on the new behavior. If it was, could you share what the motivation for it was? (so that I can see what we may need to update downstream.)

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