图像裁剪处理器,支持多种裁剪方式和多种输入输出。
细分项 | 注意与前提 |
|---|---|
费用 | 调用算子前,您需先了解使用算子时的模型调用费用,详情请参见大模型调用计费。 |
鉴权(API Key) | 调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见获取 API Key 并配置。 |
BaseURL | 调用算子前,您需要先根据您当前使用的LAS服务所在地域,了解算子调用的BaseURL,用于配置算子调用路径参数取值。 |
输入列名 | 说明 |
|---|---|
image | 包含输入图像的数组,支持URL/base64/二进制格式 |
image_name | 可选参数,包含图像标识名的数组,用于生成输出文件名。 |
包含处理结果的字典数组,每个元素包含:
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
image_suffix | str | .jpg | 输出文件的后缀名。 |
output_dir | str | "" | 保存图像的输出文件夹路径。 |
image_src_type | str | image_url | 输入图像的格式类型。 |
crop_type | str | center | 裁剪方式。
可选值: ["coordinate", "ratio", "center"] |
crop_coords | list | None | 坐标裁剪参数。 |
crop_ratio | list | [0.8, 0.8] | 比例裁剪参数。 |
crop_size | list | None | 中心裁剪参数。 |
quality | int | 85 | 保存质量参数(适配不同格式):
默认值: 85 |
method | str | 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… │ # ╰────────────────────────────────┴───────────────┴────────────────────────────────────────────╯