ValueError: assignment destination is read-only [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
2 min

banner

# ValueError: assignment destination is read-only [Solved]

The NumPy "ValueError: assignment destination is read-only" occurs when you try to assign a value to a read-only array.

To solve the error, create a copy of the read-only array and modify the copy.

You can use the flags attribute to check if the array is WRITABLE.

main.py
from PIL import Image import numpy as np img = np.array(Image.open('house.png')) print(img.flags)
The code for this article is available on GitHub

Running the code sample produces the following output:

shell
C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True # ๐Ÿ‘ˆ๏ธ must be set to True ALIGNED : True WRITEBACKIFCOPY : False

check if writeable

In your case, WRITEABLE will likely be set to False.

In older NumPy versions, you used to be able to set the flag to true by calling the setflags() method.

main.py
from PIL import Image import numpy as np img = np.array(Image.open('house.png')) print(img.flags) img.setflags(write=1)

However, setting the WRITEABLE flag to True (1) will likely fail if the OWNDATA flag is set to False.

You will likely get the following error:

  • "ValueError: cannot set WRITEABLE flag to True of this array"

To solve the error, create a copy of the array when converting it from a Pillow Image to a NumPy array.

main.py
from PIL import Image import numpy as np # โœ… Create a copy of the array when converting img = np.array(Image.open('house.png')) print(img.flags)
The code for this article is available on GitHub

Passing the Pillow Image to the numpy.array() method creates a copy of the array.

You can also explicitly call the copy() method.

main.py
from PIL import Image import numpy as np # โœ… Create copy of array when converting img = np.array(Image.open('house.png')).copy() # called copy() # C_CONTIGUOUS : True # F_CONTIGUOUS : False # OWNDATA : True # WRITEABLE : True # ๐Ÿ‘ˆ๏ธ must be set to True # ALIGNED : True # WRITEBACKIFCOPY : False print(img.flags)

check if writable is set to true

You can also call the copy() method on the Pillow Image and modify the copy.

main.py
from PIL import Image img = Image.open('house.png') # ๐Ÿ‘‡๏ธ create a copy of the image img_copy = img.copy()

The image copy isn't read-only and allows assignment.

You can change the img_copy variable without getting the "assignment destination is read-only" error.

You can also use the numpy.copy() method.

main.py
from PIL import Image import numpy as np img = Image.open('house.png') # ๐Ÿ‘‡๏ธ create a copy of the image img_copy = np.copy(img) print(img_copy)

The numpy.copy() method returns an array copy of the given object.

The only argument we passed to the method is the image.

You can safely modify the img_copy variable without running into issues.

You most likely don't want to make changes to the original image.

Creating a copy and modifying the copy should be your preferred approach.

If you got the error when using the np.asarray() method, try changing it to np.array().

Change the following:

main.py
# โ›”๏ธ Before from PIL import Image import numpy as np img = np.asarray(Image.open('house.png')) print(img.flags)

To the following:

main.py
# โœ… After from PIL import Image import numpy as np img = np.array(Image.open('house.png')) print(img.flags)
The code for this article is available on GitHub

As long as the WRITEABLE flag is set to True, you will be able to modify the array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev