图像重采样算子用于对输入图像进行尺寸重采样(仅支持降采样),并将结果保存到用户指定的 TOS 目录。支持 4 种插值算法(nearest/bilinear/bicubic/lanczos)与 .jpg / .png 输出格式,适用于图像预处理、数据标准化、离线数据集构建等场景。
target_size 的宽高不得超过原图。image_src_type=image_url:输入公网 URLimage_src_type=image_tos:输入 tos:// 地址tos_dir 必填,指定输出目录(文件夹级别)image_name 辅助命名),并追加 _resample 后缀标识.jpg / .pngtarget_dpi)细分项 | 注意与前提 |
|---|---|
费用 | 调用算子前,您需先了解使用算子时的模型调用费用,详情请参见大模型调用计费。 |
鉴权(API Key) | 调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见获取 API Key 并配置。 |
BaseURL | 调用算子前,您需要先根据您当前使用的LAS服务所在地域,了解算子调用的BaseURL,用于配置算子调用路径参数取值。 |
调用 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" } }
输入列名 | 说明 |
|---|---|
images | 包含输入图像的数组,支持URL/base64/二进制格式 |
images_name | 可选参数,包含图像标识名的数组,用于生成输出文件名。建议传入以确保文件名唯一性,且不要带后缀;如果不传入,会为无法解析出名称的图片随机生成名称。 |
包含处理结果的字典数组,每个元素包含:
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
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… │ # ╰────────────────────────────────┴────────────────┴────────────────────────────────────────╯