Skip to content

daft.functions.crop#

crop #

crop(image: Expression, bbox: tuple[int, int, int, int] | Expression) -> Expression

Crops images with the provided bounding box.

Parameters:

Name Type Description Default
image Image Expression

to crop.

required
bbox tuple[int, int, int, int] | List Expression

Either a tuple of (x, y, width, height) parameters for cropping, or a List Expression where each element is a length 4 List which represents the bounding box for the crop

required

Returns:

Name Type Description
Expression Image Expression

An expression representing the cropped image

Source code in daft/functions/image.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def crop(image: Expression, bbox: tuple[int, int, int, int] | Expression) -> Expression:
    """Crops images with the provided bounding box.

    Args:
        image (Image Expression): to crop.
        bbox (tuple[int, int, int, int] | List Expression):
            Either a tuple of (x, y, width, height)
            parameters for cropping, or a List Expression where each element is a length 4 List
            which represents the bounding box for the crop

    Returns:
        Expression (Image Expression): An expression representing the cropped image
    """
    if not isinstance(bbox, Expression):
        if len(bbox) != 4 or not all([isinstance(x, int) for x in bbox]):
            raise ValueError(f"Expected `bbox` to be either a tuple of 4 ints or an Expression but received: {bbox}")
        bbox = Expression._to_expression(bbox).cast(DataType.fixed_size_list(DataType.uint64(), 4))
    return Expression._call_builtin_scalar_fn("image_crop", image, bbox)