Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. def image_center_crop(img):
  2. """
  3. Makes a square center crop of an img, which is a [h, w, 3] numpy array.
  4. Returns [min(h, w), min(h, w), 3] output with same width and height.
  5. For cropping use numpy slicing.
  6. """
  7.  
  8. h,w,_ = img.shape
  9. length = min(h, w)
  10. l = min(h, w)
  11.  
  12. cropped_img = img[:, (w - h)//2 : (w - h)//2 + l, :] if w >= h else img[(h - w)//2 : (h - w)//2 + l, :, :]
  13.  
  14. # checks for errors
  15. h, w, c = img.shape
  16. assert cropped_img.shape == (min(h, w), min(h, w), c), "error in image_center_crop!"
  17.  
  18. return cropped_img
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement