Shifting an image in numpy
There's no other way, as to handle negative and positive shifts accordingly:
non = lambda s: s if s<0 else None
mom = lambda s: max(0,s)
ox, oy = 100, 20
shift_lena = numpy.zeros_like(lena)
shift_lena[mom(oy):non(oy), mom(ox):non(ox)] = lena[mom(-oy):non(-oy), mom(-ox):non(-ox)]
You can use roll function to circular shift x and y and then zerofill the offset
def shift_image(X, dx, dy):
X = np.roll(X, dy, axis=0)
X = np.roll(X, dx, axis=1)
if dy>0:
X[:dy, :] = 0
elif dy<0:
X[dy:, :] = 0
if dx>0:
X[:, :dx] = 0
elif dx<0:
X[:, dx:] = 0
return X