def _extract_image(img, x_off, y_off, max_x, max_y):
    """Returns a subsection of the image
    Args:
        img(numpy array): the source image (with 2 or 3 size dimensions)
        x_off(int): the starting X clip position (0th index of image)
        y_off(int): the starting Y clip position (1st index of image)
        max_x(int): the X size of extract (0th index of image)
        max_y(int): the Y size of extract (1st index of image)
    Return:
        The extracted portion of the image. If the requested extraction doesn't fit
        within the bounds of the image in any direction, the original image is returned.
    """
    dims = len(img.shape)

    # Return original if we can't fulfill the request for a dimension
    print("Extract: params: " + str(img.shape) + " " + str(x_off) + " " + str(y_off) + " " + str(max_x) + " " + str(max_y))
    if max_x == img.shape[0] and max_y == img.shape[1]:
        print("Extract:     Returning original, exact match")
        return img

    # Check if we need to clip the image from the origin because it's inherently too large
    if x_off + max_x > img.shape[0] or y_off + max_y > img.shape[1]:
        x_start = 0 if x_off + max_x > img.shape[0] else x_off
        y_start = 0 if y_off + max_y > img.shape[1] else y_off
        x_end = x_start + max_x
        y_end = y_start + max_y
        print("Extract:     Returning original, clipped: " + str(x_start) + "," + str(y_start) + " " + str(x_end) + "," + str(y_end))
        if dims == 2:
            return img[x_start:x_end, y_start:y_end]
        return img[x_start:x_end, y_start:y_end, :]

    # Return same type of image
    dims = len(img.shape)
    if dims == 2:
        return img[x_off:max_x+x_off, y_off:max_y+y_off]

    return img[x_off:max_x+x_off, y_off:max_y+y_off, :]