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

AI 数据湖服务

复制全文
图片处理
图像格式转换
复制全文
图像格式转换

算子介绍

描述

图像格式转换处理器,支持多种输入输出格式转换,可调整输出质量。

核心功能

  • 多种图像格式支持:
    • 输入:URL地址、Base64编码、二进制流
    • 输出:PNG、JPEG/JPG、WebP
  • 质量调整:
    • 支持调整有损压缩格式(JPEG、WebP)的输出质量
    • 可设置质量范围为-1-100,-1表示使用默认值
  • 智能格式处理:
    • 自动处理透明通道(JPEG不支持透明通道)
    • 自动处理色彩模式转换(如CMYK转RGB)
  • 双输出模式:
    • Base64编码直出
    • TOS/本地持久化存储

注意与前提

细分项

注意与前提

费用

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

鉴权(API Key)

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

BaseURL

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

Daft 调用

算子参数

输入

输入列名

说明

images

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

images_name

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

输出

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

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

参数

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

参数名称

类型

默认值

描述

output_format

str

"png"

输出图像的格式。
描述: 输出图像的格式,支持多种常见图像格式。
可选值: ["png", "jpg", "jpeg", "webp"]
默认值: "png"

image_suffix

str

""

保存到 TOS 或本地的图像后缀。
描述: 保存到 TOS 或本地的图像后缀,如果不设置,将根据output_format自动生成。
默认值: ""

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"

quality

int

-1

输出图像的质量(仅适用于有损压缩格式)。
描述: 输出图像的质量,范围-1-100,仅适用于jpg、jpeg、webp等有损压缩格式。
当设置为-1时,将使用各格式的内置默认质量值(JPEG默认75,WebP默认80)。
默认值: -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…         │
    # ╰────────────────────────────────┴────────────────┴────────────────────────────────────────╯
最近更新时间:2026.02.10 15:51:19
这个页面对您有帮助吗?
有用
有用
无用
无用