Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def image_center_crop(img):
- """
- Makes a square center crop of an img, which is a [h, w, 3] numpy array.
- Returns [min(h, w), min(h, w), 3] output with same width and height.
- For cropping use numpy slicing.
- """
- h,w,_ = img.shape
- length = min(h, w)
- l = min(h, w)
- cropped_img = img[:, (w - h)//2 : (w - h)//2 + l, :] if w >= h else img[(h - w)//2 : (h - w)//2 + l, :, :]
- # checks for errors
- h, w, c = img.shape
- assert cropped_img.shape == (min(h, w), min(h, w), c), "error in image_center_crop!"
- return cropped_img
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement