crop center portion of a numpy image

后端 未结 5 1425
醉梦人生
醉梦人生 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:25

    Thanks, Divakar.

    Your answer got me going the right direction. I came up with this using negative slice offsets to count 'from the end':

    def cropimread(crop, xcrop, ycrop, fn):
        "Function to crop center of an image file"
        img_pre= msc.imread(fn)
        if crop:
            ysize, xsize, chan = img_pre.shape
            xoff = (xsize - xcrop) // 2
            yoff = (ysize - ycrop) // 2
            img= img_pre[yoff:-yoff,xoff:-xoff]
        else:
            img= img_pre
        return img
    

提交回复
热议问题