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

AI 数据湖服务

复制全文
图片处理
图片重采样
复制全文
图片重采样

算子介绍

描述

图像重采样算子用于对输入图像进行尺寸重采样(仅支持降采样),并将结果保存到用户指定的 TOS 目录。支持 4 种插值算法(nearest/bilinear/bicubic/lanczos)与 .jpg / .png 输出格式,适用于图像预处理、数据标准化、离线数据集构建等场景。

使用限制

  • 需要能访问输入图像(公网 URL 或您账号下的 TOS 地址)。
  • 仅支持降采样:target_size 的宽高不得超过原图。
  • 输入图像格式支持:JPEG/PNG/TIFF(JPG/TIF 作为别名支持)。
  • 输入图像大小限制:文件大小不超过 100MB;像素总数不超过 225,000,000。

核心功能

  1. 多种插值算法
  • nearest:速度最快,适合像素风格图像
  • bilinear:速度与质量平衡
  • bicubic:更平滑的高质量缩放
  • lanczos:抗锯齿效果更好,适合照片
  1. URL / TOS 输入支持
  • image_src_type=image_url:输入公网 URL
  • image_src_type=image_tos:输入 tos:// 地址
  1. 输出到 TOS
  • tos_dir 必填,指定输出目录(文件夹级别)
  • 输出文件名由服务端生成(可通过 image_name 辅助命名),并追加 _resample 后缀标识
  1. 输出格式与 DPI 控制
  • 输出格式支持 .jpg / .png
  • 支持设置输出 DPI(target_dpi

注意与前提

细分项

注意与前提

费用

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

鉴权(API Key)

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

BaseURL

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

Rest API 调用

接口说明

调用 las_image_resample 算子对输入图像进行重采样(仅支持降采样),并将结果保存到用户指定的 TOS 目录。

请求参数

参数
类型
必填
示例值
说明
operator_id
string
las_image_resample
本算子的 id 是 las_image_resample。
operator_version
string
v1
目前只支持 v1。
data
process_param
算子参数。

返回数据

参数
类型
示例值
说明
metadata
metadata
请求的元信息。
data
result
返回的数据,随着不同算子有不同模式。

示例

请求示例

# 请将 INPUT_IMAGE_URL 设置为可访问的图片 URL
export INPUT_IMAGE_URL="https://example.com/sample.jpg"
# 请将 OUTPUT_TOS_DIR 设置为本账号上可写入的 TOS 目录(文件夹)
export OUTPUT_TOS_DIR="tos://xxxx/las_image_resample/output"

curl --location "https://operator.las.cn-beijing.volces.com/api/v1/process" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $LAS_API_KEY" \
--data '{
  "operator_id": "las_image_resample",
  "operator_version": "v1",
  "data": {
    "image_src_type": "image_url",
    "image": "$INPUT_IMAGE_URL",
    "tos_dir": "$OUTPUT_TOS_DIR",
    "image_suffix": ".jpg",
    "target_size": [1024, 1024],
    "target_dpi": [72, 72],
    "method": "lanczos"
  }
}'

返回示例

{
    "metadata": {
        "task_status": "COMPLETED",
        "business_code": "0",
        "error_msg": "",
        "request_id": "c7b29d78a99f88beda5497753ed60816"
    },
    "data": {
        "image_path": "tos://xxxx/las_image_resample/output/sample_resample.jpg"
    }
}

Daft 调用

算子参数

输入

输入列名

说明

images

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

images_name

可选参数,包含图像标识名的数组,用于生成输出文件名。建议传入以确保文件名唯一性,且不要带后缀;如果不传入,会为无法解析出名称的图片随机生成名称。

输出

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

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

参数

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

参数名称

类型

默认值

描述

image_suffix

str

.jpg

保存到 TOS 中的图像格式。 描述: 保存到 TOS 中的图像格式,默认为'.jpg' 可选值: [".jpg", ".png"] 默认值: ".jpg"

tos_dir

str

保存图像的 TOS 文件夹路径。 描述: 将重采样后的图像保存到的 TOS 路径,如果不设置,则不会将重采样后的图像保存到 TOS 中。 默认值: ""

local_dir

str

保存图像的本地文件夹路径。 描述: 调整后图像的本地存储目录,如果为空,则调整后的图像不保存到本地。 默认值: ""

image_src_type

str

image_url

输入图像的格式类型,支持: - url地址(image_url) - base64编码(image_base64) - 二进制流(image_binary) 描述: 输入图像的格式类型。 可选值: ["image_url", "image_base64", "image_binary"] 默认值: "image_url"

target_size

list

[200, 200]

重采样后的图像尺寸。 描述: 重采样后的图像尺寸,格式为 [width, height],默认为 [200, 200]。 默认值: [200, 200]

target_dpi

list

[72, 72]

图像 DPI。 描述: 图像 DPI,格式为 [width, height],默认为 [72, 72]。 默认值: [72, 72]

method

str

lanczos

重采样方法。 描述: 重采样方法,支持 nearest(最近邻插值)、bilinear(双线性插值)、bicubic(双三次插值)、lanczos(Lanczos插值),默认为 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_resample import ImageResample
from daft.las.functions.udf import las_udf

if __name__ == "__main__":
    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.context.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_ip_adapter.jpeg"
        ],
        "image_name": ["cat_ip_adapter"],
    }

    image_suffix = ".jpg"
    image_src_type = "image_url"
    target_size = (200, 200)
    target_dpi = (72, 72)
    method = "lanczos"
    local_dir = ""
    tos_dir = ""
    num_gpus = 0

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "image_resample",
        las_udf(
            ImageResample,
            construct_args={
                "image_suffix": image_suffix,
                "tos_dir": tos_dir,
                "local_dir": local_dir,
                "image_src_type": image_src_type,
                "target_size": target_size,
                "target_dpi": target_dpi,
                "method": method,
            },
            num_gpus=num_gpus,
            batch_size=1,
        )(col("image"), col("image_name")),
    )

    ds.show()

    # ╭────────────────────────────────┬────────────────┬────────────────────────────────────────╮
    # │ image                          ┆ image_name     ┆ image_resample                         │
    # │ ---                            ┆ ---            ┆ ---                                    │
    # │ Utf8                           ┆ Utf8           ┆ Struct[base64: Utf8, image_path: Utf8] │
    # ╞════════════════════════════════╪════════════════╪════════════════════════════════════════╡
    # │ https://las-cn-beijing-public… ┆ cat_ip_adapter ┆ {base64: iVBORw0KGgoAAAANSUhE…         │
    # ╰────────────────────────────────┴────────────────┴────────────────────────────────────────╯
最近更新时间:2026.02.27 19:49:04
这个页面对您有帮助吗?
有用
有用
无用
无用