np arrays being immutable - "assignment destination is read-only"
Question: FD** - I am a Python newb as well as a stack overflow newb as you can tell. I
have edited the question based on comments.

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

Thanks @user2314737. I will attempt to set that flag. @Eric, thanks for your
comments as well.

    
    
    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.

Thank you everyone for help.


Answer: Check if the array is writable with

    
    
    >>> img.flags
      C_CONTIGUOUS : True
      F_CONTIGUOUS : False
      OWNDATA : True
      WRITEABLE : False
      ALIGNED : True
      UPDATEIFCOPY : False
    

If `WRITEABLE`is false, change it with

    
    
    img.setflags(write=1)
    

