def boxRatioIsEqual(box: BoundingBox, aspectRatio: float): """Checks if a boxes aspect ratio is equal to the given aspect ratio. Args: box (BoundingBox): box to check aspect ratio for aspectRatio (Tuple[int,int]): aspect ratio to check box against (width, height) """ diff = abs(box.aspectRatio() - aspectRatio) return diff < epsilon
def getSizeFromRatio(boundingBox: BoundingBox, aspectRatio: float) -> Tuple[float, float, bool]: """Given a bounding box and desired aspect ratio, this function calculates the closest pixel size (width and height) that that is larger than the bounding box and at the correct aspect ratio. Returns the width, height, and a boolean value indicating wheter the new aspect ratio is wider or not Args: BoundingBox (BoundingBox): bounding box representing the smallest size aspectRatio (float): desired aspect ratio Returns: Tuple[float,float,bool]: width, height, isWider """ if boundingBox.aspectRatio( ) > aspectRatio: # The box is wider than we need height = boundingBox.width() / aspectRatio return boundingBox.width(), height, False elif boundingBox.aspectRatio( ) < aspectRatio: # The box is taller than we need width = boundingBox.height() * aspectRatio return width, boundingBox.height(), True