1

I am trying to copy a section of an input 2d array "img" and mirroring that section and copying it into the 2d array "out"

The following code does what I need

a = numpy.zeros(shape=(pad, pad))
a[:,:]=img[0:pad,0:pad]
out[0:pad,0:pad]=a[::-1,::-1]

But simply doing the following does not

out[0:pad,0:pad]=img[0:pad:-1,0:pad:-1]

and instead returnsValueError: could not broadcast input array from shape (0,0) into shape (2,2) for pad=2 and I am not sure why.

1 Answer 1

1
img[0:pad:-1,0:pad:-1] 

should be

img[pad-1::-1, pad-1::-1]

since you want the index to start at pad-1 and step down to 0. See here for the complete rules governing NumPy basic slicing.

For example,

import numpy as np

img = np.arange(24).reshape(6,4)
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19],
#        [20, 21, 22, 23]])

pad = 2
out = img[pad-1::-1, pad-1::-1]

print(out)

yields

[[5 4]
 [1 0]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.