3

I got stuck practising with images in Python 3:

import numpy as np
from matplotlib.image import imread
photo_data = imread('c:\jpeg.jpg')
photo_data[0,0,1] = 0

I get this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-f19579124b68> in <module>()
      1 photo = photo_data
      2 print(type(photo))
----> 3 photo[0,0,1] = 0
      4 plt.imshow(photo_data)

ValueError: assignment destination is read-only

I'm following an online course where this code seems working, can you tell me what I'm getting wrong?

0

2 Answers 2

6

The issue at hand is that the array is set by matplotlib to read-only. To confirm:

print(photo_data.flags)

And you will get:

C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : False
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

To make it writable, simply:

photo_data.setflags(write=1)
photo_data[0,0,1] = 0
Sign up to request clarification or add additional context in comments.

Comments

4

some times, you will get error as below if you try to set write flag to True.

ValueError: cannot set WRITEABLE flag to True of this array

Just make copy of it and work. it is useful instead downgrading the numpy version

photo = photo_data.copy() print(type(photo)) photo[0,0,1] = 0 plt.imshow(photo_data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.