0
import numpy as np 
import matplotlib.pyplot as plt
from PIL import Image

pic = Image.open('mountain1.jpg')
pic_array = np.asarray(pic)
# plt.imshow(pic_array[:,:,2], cmap='gray')
pic_array[:,:,2]=0
plt.imgshow(pic_array)
plt.show()

i get the following error/ pic_array[:,:,2]=0 ValueError: assignment destination is read-only How do i edit the array?

1 Answer 1

1

The problem is that in the code the original image and the numpy array share the same memory. hence the read-only error when you try to update the array.

Create the copy as the original array and it should be fine.

Another small thing that I noticed is plt.imgshow(image) is used instead of plt.imshow(image).

import numpy as np 
import matplotlib.pyplot as plt
from PIL import Image

pic = Image.open('mountain1.jpg')
# copy of the numpy array so that the original image is not changed.
pic_array = np.asarray(pic).copy()
pic_array[:,:,2]=0
plt.imshow(pic_array)

Cheers!

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.