def crop(filepath: str, amount: Any = None) -> cv2.Mat:
"""
Read the image at filepath, crop according to `amount`, and return a opencv
image. Amount is the number of pixels **from the edge** to remove. It is given
as a single integer or a list of integers with length 1, 2, or 4. A single
value applies the crop to all sides. 2 values applies to vertical and
horizontal sides `[top+botton, left+right]`. 4 values applies to each side
`[top, right, bottom, left]`. This is the same ordering as CSS properties such
as `margin`.
"""
img = cv2.imread(filepath)
if not amount:
return img
d = expand_crop_amount(amount) # convert amount to full 4 number form
h = img.shape[0]
w = img.shape[1]
c = [0+d[3], w-d[1], 0+d[0], h-d[2]] # CSS order style is goofy
crop = img[c[2]: c[3], c[0]: c[1]] # y is first, x is second
return crop
def resize(img: cv2.Mat, proportion: float = 1) -> cv2.Mat:
"""Resize/scale img by proprotion."""
return cv2.resize(src=img, dsize=(0, 0), fx=proportion,
fy=proportion, interpolation=cv2.INTER_AREA)