61

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.

  • there are numerous web links http://stackoverflow.com/questions/13572448/change-values-in-a-numpy-array suggests at least two – NaN Sep 18 '16 at 07:36
  • Where did `img` come from, and why do you need `asarray`? – Eric Sep 18 '16 at 09:01
  • 4
    This whole piece of code can be better written `prArray = prArray != 0` – Eric Sep 18 '16 at 09:02
  • Using `np.array()` instead of `np.asarray()` likely solves this – gebbissimo Mar 31 '21 at 12:05
  • If you want to make your array **writable**, You must make a copy (copy_arr = prArray.copy()) of your array, and then you can change or write the elements of your "copy array" – Ali_Baghayeri Jan 27 '23 at 08:27

6 Answers6

74

Check if the array is writable with

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

If WRITEABLEis false, change it with

img.setflags(write=1)
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 27
    There might be a good reason that the array is readonly - it'd be worth trying to work out why the producer of `img` decided the result is readonly before changing the flags. If the reason is a sound one, consider using `img = img.copy()` instead – Eric Sep 18 '16 at 10:06
  • 16
    `ValueError: cannot set WRITEABLE flag to True of this array`. Drat! – Monica Heddneck Mar 22 '20 at 00:44
  • 1
    @MonicaHeddneck see 2nd answer by "AleksMat" – Nathan majicvr.com Jun 02 '20 at 19:51
  • More cmd line options for `img.setflags(params)` from [np 1.18 documentation](https://numpy.org/doc/1.18/reference/generated/numpy.ndarray.setflags.html): `1:` writebool, optional; describes whether or not a can be written to. `2:` alignbool, optional; describes whether or not a is aligned properly for its type. `3.` uicbool, optional. Describes whether or not a is a copy of another “base” array. – Nathan majicvr.com Jun 02 '20 at 19:52
63

Since numpy version 1.16.0 the following doesn't work anymore:

img = np.asarray(Image.open(filename))
img.setflags(write=1)

The problem is that now OWNDATA is set to False and you can't set WRITEABLE flag to True. Therefore you should simply do the following:

img = np.array(Image.open(filename))

This will make a copy of array when casting it from Pillow object to numpy array. However I tested time performance in numpy 1.16.0 and haven't found any noticable difference between both methods.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
AleksMat
  • 854
  • 6
  • 11
  • This will definitely make a copy of the array so there isn't any doubt in your statement. I've edited the language accordingly to make this an assertion rather than a doubting thought. – rayryeng Dec 03 '19 at 18:13
  • 1
    Kind of "antipythonic," right? Empower people; don't protect them from potential mistakes! – Nathan majicvr.com Jun 02 '20 at 23:25
  • I got an error when trying to `setflags(write=1)`. The way I solved it was with: `img = np.asarray(Image.open(filename)).copy` – zanbri Jul 18 '20 at 10:58
8

In this case, I think you are trying to edit the image provided to you by another user and he/she made it uneditable that's why you are getting this error. For your case, you may try to make a copy of the given file and do changes on that file by using .copy().

     img_copy = img.copy()
     prArray = [np.asarray(img_copy)[:, :, 0] for img_copy in problem_images]

And more importantly, I do not think that most of us want to make changes to our original image, that's why I always use .copy() and recommend you to do the same.

5
ValueError: cannot set WRITEABLE flag to True of this array array.setflags(write=1) 

Skip the statement using copy the array using np.copy()

cizario
  • 3,995
  • 3
  • 13
  • 27
keyan wilson
  • 51
  • 1
  • 2
1

I recommend that you stop doing math on the original image. You can solve this problem by using img.copy()

img = plt.imread('../img/1.2.jpg')
img=img.copy()
  • While this code snippet may be the solution, including a detailed explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Feb 26 '23 at 01:38
1

I think you don't need to downgrade your numpy version. You have just to make a copy of the original image like this :

img_copy = np.copy(img)

and it works !

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • Is there any reason you are suggesting the use of `np.copy(img)` instead of `img.copy()`? – Joshua Shew Aug 11 '23 at 02:46
  • Oh, I saw some people suggesting to downgrade numpy that is why I put this script to emphasize the fact that we don't need to downgrade it. Then, since many people are more familiar with numpy, in this particular case, I think they don't need to import another library Thanks – Youssef Zied ELHECHMI Aug 25 '23 at 14:09