<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def crop_image(img, padding=5):
    "Crops an image or slice to its extents"

    if padding < 1:
        return img

    beg_coords, end_coords = crop_coords(img, padding)

    if len(img.shape) == 3:
        img = crop_3dimage(img, beg_coords, end_coords)
    elif len(img.shape) == 2:
        img = crop_2dimage(img, beg_coords, end_coords)
    else:
        raise ValueError('Can only crop 2D or 3D images!')

    return img