You need to enable JavaScript to run this app.
AI 数据湖服务

AI 数据湖服务

复制全文
图片处理
图片裁剪
复制全文
图片裁剪

算子介绍

描述

图像裁剪处理器,支持多种裁剪方式和多种输入输出。

核心功能

  • 输入格式:
    • URL 地址(image_url)
    • Base64 编码(image_base64)
    • 二进制流(image_binary)
  • 裁剪方式(支持多种算法/模式):
    • 坐标裁剪:指定左上角/右下角坐标精准裁剪
    • 比例裁剪:按宽高比例裁剪(0-1)
    • 中心裁剪:按指定尺寸从图像中心裁剪
  • 插值算法:nearest / bilinear / bicubic / lanczos(裁剪后尺寸适配时使用)
  • 输出模式:
    • 返回裁剪后图片的 Base64 字符串
    • 可选将图片保存到 TOS
    • 可选将图片保存到本地目录
  • 格式适配:
    • JPEG:RGBA 转 RGB + 质量压缩
    • PNG:保留透明通道 + 无损压缩
    • WebP:支持透明通道 + 质量/无损压缩

注意与前提

细分项

注意与前提

费用

调用算子前,您需先了解使用算子时的模型调用费用,详情请参见大模型调用计费

鉴权(API Key)

调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见获取 API Key 并配置

BaseURL

调用算子前,您需要先根据您当前使用的LAS服务所在地域,了解算子调用的BaseURL,用于配置算子调用路径参数取值。
详情请参见获取 Base URL,下文中的调用示例仅作为参考,实际调用时需替换为您对应地域的路径取值。

Daft 调用

算子参数

输入

输入列名

说明

image

包含输入图像的数组,支持URL/base64/二进制格式

image_name

可选参数,包含图像标识名的数组,用于生成输出文件名。

输出

包含处理结果的字典数组,每个元素包含:

  • base64: 裁剪后图像的base64编码;
  • image_path: 本地/TOS存储路径(当配置输出目录时有效)

参数

如参数没有默认值,则为必填参数

参数名称

类型

默认值

描述

image_suffix

str

.jpg

输出文件的后缀名。
描述: 保存到 TOS / 本地 时使用的文件后缀,需为支持的格式。
可选值: [".jpg", ".jpeg", ".png", ".webp"]
默认值: ".jpg"

output_dir

str

""

保存图像的输出文件夹路径。
描述: 将裁剪后的图像保存到的输出路径,为空则不保存到输出,支持 TOS 和本地路径。
默认值: ""

image_src_type

str

image_url

输入图像的格式类型。
可选值: ["image_url", "image_base64", "image_binary"]
默认值: "image_url"

crop_type

str

center

裁剪方式。
描述: 支持三种裁剪模式,需配合对应参数使用:

  • coordinate: 需指定 crop_coords=[x1, y1, x2, y2]
  • ratio: 需指定 crop_ratio=[width_ratio, height_ratio](0-1)
  • center: 需指定 crop_size=[width, height](或使用默认比例)

可选值: ["coordinate", "ratio", "center"]
默认值: "center"

crop_coords

list

None

坐标裁剪参数。
描述: [x1, y1, x2, y2],x1/y1=左上角坐标,x2/y2=右下角坐标。
默认值: None

crop_ratio

list

[0.8, 0.8]

比例裁剪参数。
描述: [width_ratio, height_ratio],值范围 0-1(如 [0.8, 0.8] 裁剪80%区域)。
默认值: [0.8, 0.8]

crop_size

list

None

中心裁剪参数。
描述: [width, height],指定裁剪后的宽高,None 则按原尺寸80%裁剪。
默认值: None

quality

int

85

保存质量参数(适配不同格式):

  • JPEG/WebP:1-100(数值越高质量越好)
  • PNG:映射为 compress_level(1→9,100→0)

默认值: 85

method

str

lanczos

插值算法(裁剪后尺寸适配时使用)。
可选值: ["nearest", "bilinear", "bicubic", "lanczos"]
默认值: "lanczos"

调用示例

下面的代码展示了如何使用 daft 运行算子对图像做裁剪。

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.image.image_crop import ImageCrop
from daft.las.functions.udf import las_udf

if __name__ == "__main__":
    # 内容会保存到指定的TOS路径下,因此,需要设置好环境变量以保证有权限写入TOS,包括: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_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/cat.png"],
        "image_name": ["cat_crop_test"],
    }

    image_suffix = ".jpg"
    image_src_type = "image_url"
    crop_type = "center"
    crop_size = [500, 500]
    crop_ratio = [0.8, 0.8]
    crop_coords = [100, 100, 800, 800]
    quality = 85
    method = "lanczos"
    local_dir = ""

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "image_crop",
        las_udf(
            ImageCrop,
            construct_args={
                "image_suffix": image_suffix,
                "output_dir": output_tos_dir,
                "image_src_type": image_src_type,
                "crop_type": crop_type,
                "crop_size": crop_size,
                "quality": quality,
                "method": method,
            },
            batch_size=1,
        )(col("image"), col("image_name")),
    )
    ds.show()
# ╭────────────────────────────────┬───────────────┬────────────────────────────────────────────╮
# │ image                          ┆ image_name    ┆ image_crop                                 │
# │ ---                            ┆ ---           ┆ ---                                        │
# │ String                         ┆ String        ┆ Struct[base64: String, image_path: String] │
# ╞════════════════════════════════╪═══════════════╪════════════════════════════════════════════╡
# │ https://las-ai-qa-online.tos-… ┆ cat_crop_test ┆ {base64: /9j/4AAQSkZJRgABAQAA…             │
# ╰────────────────────────────────┴───────────────┴────────────────────────────────────────────╯
最近更新时间:2026.02.10 15:51:19
这个页面对您有帮助吗?
有用
有用
无用
无用