0

Why is the numpy array immutable after being read using matplotlib.pyplot.imread? What is the reasoning behind that?

>>> import matplotlib
>>> test=matplotlib.pyplot.imread('download.jpeg')
>>> test.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
2
  • Where is the array? Commented Aug 24, 2018 at 18:17
  • 1
    @Bazingaa: The array is test. Commented Aug 24, 2018 at 18:18

1 Answer 1

4

In terms of what happens in the code, matplotlib.pyplot.imread delegates to Pillow to read the file and then calls np.asarray on the resulting PIL.Image.Image object. When converting a PIL.Image.Image to a NumPy array, the Image object builds a bytestring to use as the array's buffer. Bytestrings are immutable, so the resulting array is unwriteable.

In terms of why anyone on the Matplotlib or Pillow dev team chose an implementation that results in an unwriteable array, I don't know. It's not clear whether this was deliberate at all.

If you want a writeable array, call the array's copy method:

mutable_array = matplotlib.pyplot.imread('download.jpeg').copy()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.