My goal is to read a set of PNG files, create Image(s) with Image.open('filename')
and convert them to simple 2D arrays with only 1s and 0s. The PNG is of the format RGBA with mostly only 255 and 0 as values. Quite often in the images, the edges are grey scale values, which I would like to avoid in the 2D array.
I created the 2D array from image using np.asarray(Image)
getting only the 'Red' channel. In each of the 2d image array, I would like to set the cell value = 1 if the current value is non zero.
So, I loop into the 2d array and I check the cell value and try to set it to 1.
It gives me an error indicating that the array is read-only. I read through several stack overflow threads discussing that np arrays are immutable and it is a still bit unclear. I use PIL and numpy.
from PIL import Image
import numpy as np
The relevant code:
prArray = [np.asarray(img)[:, :, 0] for img in problem_images]
for img in prArray:
for x in range(184):
for y in range(184):
if img[x][y] != 0:
img[x][y] = 1
The error "assignment destination is read-only" is in the last line.
img
come from, and why do you needasarray
? – GrogramprArray = prArray != 0
– Grogramnp.array()
instead ofnp.asarray()
likely solves this – Laughton