Home > Net >  ValueError: assignment destination is read-only
ValueError: assignment destination is read-only

Time:10-10

My goal is to make aliasing in python but actually i have problem ValueError: assignment destination is read-only. This because of this line

numpy_new[:lines * k][:][:] = numpy_last

And I don't know how solve this.

import warnings
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation, FuncAnimation
from PIL import Image
warnings.filterwarnings("ignore", category=DeprecationWarning)
N = 3
M = 64
PI = np.pi
x = np.linspace(1, 10, 1000)
y_ = np.sin((N * x   (1 * PI) / 10))
subplot_kw = {'projection': 'polar'}
fig, ax = plt.subplots()
lines = 16
sensor_img = []
for i in range(M):
    k = i
    if i == 0:
        image = Image.open(f'proppler/myplot{i}.png')
        sensor_img.append(image)
        image.save(fr'Sensor/sensor{i}.png')
        continue
    last = sensor_img[k - 1]
    numpy_last = np.asarray(last)[:lines * k][:][:]
    new_image = Image.open(fr'proppler/myplot{i}.png')
    numpy_new = np.asarray(new_image)

    numpy_new[:lines * k][:][:] = numpy_last
    result = Image.fromarray(numpy_new)
    result.save(fr'Sensor/sensor{i}.png')
    if k == 16:
        k = 0
    sensor_img.append(result)

ims = []
for i in range(M):
    im_file = plt.imread(f'Sensor/sensor{i}.png')
    im = ax.imshow(im_file)
    ims.append([im])
    if i == 64:
        break

plt.grid(False)
plt.axis('off')

ani = ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=0)
plt.show()

Thank you everyone for help.

CodePudding user response:

You can try changing numpy_last = np.asarray to numpy_last = np.array as suggested here.

If you included images I could test it but now it's just a guess

As for testing I did the syntax seems correct and I could force read_only error

import numpy as np

b = np.zeros((2, 2))   1
a = np.zeros((3, 3))
c = np.asarray(a)

print(a, b, c, sep="\n")

c[:2, :2] = b
a[:2, :2] = b

print("----")
print(a, b, c, sep="\n")

print("Forced error \n")
c.setflags(write=0)
c[:2, :2] = b

Output:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[1. 1.]
 [1. 1.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
----
[[1. 1. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
[[1. 1.]
 [1. 1.]]
[[1. 1. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
Force an error 

Traceback (most recent call last):
  File "/tmp/n = int(input("Enter an integer: "))", line 17, in <module>
    c[:2, :2] = b
ValueError: assignment destination is read-only
  • Related