图像格式转换处理器,支持多种输入输出格式转换,可调整输出质量。
细分项 | 注意与前提 |
|---|---|
费用 | 调用算子前,您需先了解使用算子时的模型调用费用,详情请参见大模型调用计费。 |
鉴权(API Key) | 调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见获取 API Key 并配置。 |
BaseURL | 调用算子前,您需要先根据您当前使用的LAS服务所在地域,了解算子调用的BaseURL,用于配置算子调用路径参数取值。 |
输入列名 | 说明 |
|---|---|
images | 包含输入图像的数组,支持URL/base64/二进制格式 |
images_name | 可选参数,包含图像标识名的数组,用于生成输出文件名。建议传入以确保文件名唯一性,且不要带后缀;如果不传入,会为无法解析出名称的图片随机生成名称。 |
包含处理结果的字典数组,每个元素包含:
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
output_format | str | "png" | 输出图像的格式。 |
image_suffix | str | "" | 保存到 TOS 或本地的图像后缀。 |
tos_dir | str | 保存图像的 TOS 文件夹路径。 | |
local_dir | str | 保存图像的本地文件夹路径。 | |
image_src_type | str | "image_url" | 输入图像的格式类型,支持:
描述: 输入图像的格式类型。 |
quality | int | -1 | 输出图像的质量(仅适用于有损压缩格式)。 |
下面的代码展示了如何使用 daft 运行算子对图像做格式转换。
from __future__ import annotations import os import daft from daft import col from daft.las.functions.image.image_format_convert import ImageFormatConvert 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.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"], } output_format = "png" image_suffix = "" tos_dir = "" local_dir = "" image_src_type = "image_url" quality = -1 num_gpus = 0 ds = daft.from_pydict(samples) ds = ds.with_column( "image_format_convert", las_udf( ImageFormatConvert, construct_args={ "output_format": output_format, "image_suffix": image_suffix, "tos_dir": tos_dir, "local_dir": local_dir, "image_src_type": image_src_type, "quality": quality, }, num_gpus=num_gpus, batch_size=1, )(col("image"), col("image_name")), ) ds.show() # ╭────────────────────────────────┬────────────────┬────────────────────────────────────────╮ # │ image ┆ image_name ┆ image_format_convert │ # │ --- ┆ --- ┆ --- │ # │ Utf8 ┆ Utf8 ┆ Struct[base64: Utf8, image_path: Utf8] │ # ╞════════════════════════════════╪════════════════╪════════════════════════════════════════╡ # │ https://las-cn-beijing-public… ┆ cat_ip_adapter ┆ {base64: iVBORw0KGgoAAAANSUhE… │ # ╰────────────────────────────────┴────────────────┴────────────────────────────────────────╯