You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Image processing
Image black border cropping
Copy page
Download PDF
Image black border cropping

Operator ID: daft.las.functions.image.image_blackborder_crop.ImageBlackBorderCrop

Operator introduction

Description

Image black border detection and cropping processor, supports multiple detection algorithms and output formats.

Key features

  • Four professional-grade black border detection algorithms:
    • Threshold detection (threshold) – fastest speed, suitable for pure black borders or solid color borders
    • Edge detection (edge) – based on Canny edge detection, suitable for blurred or gradient black borders
    • Histogram detection (histogram) – based on pixel distribution, suitable for low-contrast black borders
    • Automatic detection (auto) – intelligently selects the optimal algorithm (recommended)
  • Multi-format input support:
    • URL address (image_url)
    • Base64 encoding (image_base64)
    • Binary stream (image_binary)
  • Multi-format output support:
    • JPEG/JPG: Automatically converts RGBA to RGB, preserves DPI
    • PNG: Preserves transparency channel, lossless saving
    • WebP: Supports transparency channel, flexible saving
  • Dual output modes:
    • Direct Base64 encoding output
    • TOS/local persistent storage
  • Robustness assurance:
    • Minimum black border size filtering (prevents excessive cropping)
    • Boundary validation (prevents cropping beyond image boundaries)
    • Automatic fallback to original image if detection fails

Caution and prerequisites

Details

Caution and prerequisites

Costs

Before calling an operator, you need to understand the model invocation costs associated with using the operator. For details, see Large model invocation billing.

Authentication (API Key)

Before calling an operator, you need to generate an API Key for operator invocation. It is recommended to configure the API Key as an environment variable to ensure safer operator calls. For details, see Obtain and configure API Key.

BaseURL

Before calling an operator, you need to determine the BaseURL for operator invocation based on the region where your current LAS service is deployed. This is used to configure the path parameter values for operator calls.
For details, see Obtain the Base URL. The Examples below are for reference only; when making actual calls, replace the path values with those corresponding to your region.

Daft invocation

Operator parameters

Input

Input column name

Note

image

An array containing input images, supports URL, Base64, or binary formats

image_name

Optional parameter, an array of image identifiers, used to generate output file names.

Output

An array of dictionaries containing processing results; each element includes:

  • base64: Base64 encoding of the cropped image;
  • image_path: Local/TOS storage path (effective when output directory is configured)

Parameters

If a parameter does not have a default value, it is required.

Parameter name

Type

Default value

Description

image_suffix

str

.jpg

Format for saving images to TOS or local storage.
Description: The save format for the cropped image, supports multiple formats.
Optional values: [".jpg", ".jpeg", ".png", ".webp"]
Default value: ".jpg"

output_dir

str

""

Output folder path for saving images.
Description: The output path where the cropped image will be saved. If left empty, the cropped image will not be saved. Supports TOS and local paths.
Default value: ""

image_src_type

str

image_url

Input image format type.
Description: Input image format type, supports URL, Base64, or binary.
Optional values: ["image_url", "image_base64", "image_binary"]
Default value: "image_url"

detect_algorithm

str

auto

Black border detection algorithm.
Description: Supports four detection algorithms. The auto mode automatically selects the optimal solution.
Optional values: ["threshold", "edge", "histogram", "auto"]
Default value: "auto"

black_threshold

int

10

Grayscale threshold (effective only for threshold/auto algorithms).
Description: Pixels with grayscale values lower than this value are considered black borders (0-255). The lower the value, the stricter the detection.
Default value: 10

edge_sensitivity

float

1.0

Edge detection sensitivity (effective only for edge/auto algorithms).
Description: Higher sensitivity makes it easier to detect weak edges (0.1-2.0).
Default value: 1.0

min_border_size

int

1

Minimum black border size (pixels).
Description: Black borders smaller than this size will not be cropped to avoid excessive cropping.
Default value: 1

target_dpi

list

[72, 72]

Image DPI.
Description: DPI parameter when saving images, formatted as [width, height].
Default value: [72, 72]

quality

int

85

Save quality parameter (adapted for different formats):

  • JPEG/WebP: 1-100 (higher values indicate better quality)
  • PNG: Mapped to compress_level (1→9, 100→0)

Default value: 85

Examples

The following code demonstrates how to use daft to run the operator to crop black borders from images.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.image.image_blackborder_crop import ImageBlackBorderCrop
from daft.las.functions.udf import las_udf

if __name__ == "__main__":
    # Content will be saved to the specified TOS path. Therefore, you need to set environment variables to ensure you have permission to write to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR
    TOS_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket")
    output_tos_dir = f"tos://{TOS_DIR}/image/image_blackborder_crop"

    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S.%s".format(),
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.set_runner_ray()

    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    samples = {
        "image": [f"https://{tos_dir_url}/public/shared_image_dataset/test_blackborder.png"],
        "image_name": ["cat_with_border_crop"],
    }

    image_suffix = ".jpg"
    image_src_type = "image_url"
    detect_algorithm = "auto"
    black_threshold = 10
    edge_sensitivity = 1.0
    min_border_size = 1
    target_dpi = [72, 72]
    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "image_blackborder_crop",
        las_udf(
            ImageBlackBorderCrop,
            construct_args={
                "image_suffix": image_suffix,
                "output_dir": output_tos_dir,
                "image_src_type": image_src_type,
                "detect_algorithm": detect_algorithm,
                "black_threshold": black_threshold,
                "edge_sensitivity": edge_sensitivity,
                "min_border_size": min_border_size,
                "target_dpi": target_dpi,
            },
            batch_size=1,
        )(col("image"), col("image_name")),
    )

    ds.show()
# ╭────────────────────────────────┬──────────────────────┬────────────────────────────────────────────╮
# │ image                          ┆ image_name           ┆ image_blackborder_crop                     │
# │ ---                            ┆ ---                  ┆ ---                                        │
# │ String                         ┆ String               ┆ Struct[base64: String, image_path: String] │
# ╞════════════════════════════════╪══════════════════════╪════════════════════════════════════════════╡
# │ https://las-ai-qa-online.tos-… ┆ cat_with_border_crop ┆ {base64: /9j/4AAQSkZJRgABAQEA…             │
# ╰────────────────────────────────┴──────────────────────┴────────────────────────────────────────────╯
Last updated: 2026.05.24 15:20:02