Let\'s say I have a numpy image of some width x and height y. I have to crop the center portion of the image to width cropx and height cropy. Let\'s assume that cropx and cr
A simple modification from @Divakar 's answer that preserves the image channel:
def crop_center(self, img, cropx, cropy): _, y, x = img.shape startx = x // 2 - (cropx // 2) starty = y // 2 - (cropy // 2) return img[:, starty:starty + cropy, startx:startx + cropx]