crop center portion of a numpy image

后端 未结 5 1444
醉梦人生
醉梦人生 2020-12-08 13:52

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

5条回答
  •  忘掉有多难
    2020-12-08 14:15

    Another simple modification from @Divakar's answer to preserve color channels:

    def crop_center(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,:]
    

提交回复
热议问题