What is np.copy actually doing?

In Lab2, exploring convolutions, we are told that we need to get the transformed image by copying the ascent_image using numpy.

image_transformed = np.copy(ascent_image)

But what is that actually doing? The program will run fine if you just use the command:

image_transformed = ascent_image

However, the effect of the convolution is completely different. What is np.copy actually doing?

There is a big difference in the result between those two statements. Remember that all the variables there are python “object references”. That means they are really what you would call “pointers” in c or C++. So in the second statement, you end up with two variables that both reference (point to) the same actual object (numpy array) in memory. So if you modify the transformed image, you’re also modifying the ascent image. In the first case, you get two separate objects in memory that you can modify independently. That’s the purpose of the np.copy. Here’s a thread which gives a concrete demonstration of the difference between the two cases.

Which version of the statement you use depends on what your intent is.