I start with a 2D array and want to broadcast it to a 3D array (eg. from greyscale image to rgb image). This is the code I use.
>>> img_grey = np.random.randn(4, 4)
>>> img_rgb = np.broadcast_to(np.expand_dims(img_grey, axis=-1), (4, 4, 3))
This creates an array img_rgb
that works as expected: 3 colored channels (last dimension), each slice being equal to the original greyscale image. However, if I do
>>> img_rgb[0, 0, 0] = 0.
ValueError: assignment destination is read-only
I can't change the rgb image!