You need to enable JavaScript to run this app.
文档中心
AI 数据湖服务

AI 数据湖服务

复制全文
图片处理
图片压缩
复制全文
图片压缩

算子ID:daft.las.functions.image.image_compress.ImageCompress

算子介绍

描述

图像压缩处理器,支持多种插值算法和多种输入输出。

核心功能

  • 输入格式:
    • URL 地址(image_url)
    • Base64 编码(image_base64)
    • 二进制流(image_binary)
  • 压缩与缩放:
    • 质量压缩(quality 参数)
    • 尺寸缩放(target_size 或保持原尺寸)
    • 多种插值算法:nearest / bilinear / bicubic / lanczos
  • 输出模式:
    • 返回压缩后图片的 Base64 字符串
    • 可选将图片保存到 TOS
    • 可选将图片保存到本地目录
  • 格式适配:
    • JPEG:质量压缩 + RGBA 转 RGB
    • PNG:无损压缩(compress_level) + 保留透明通道
    • WebP:质量/无损压缩 + 保留透明通道

注意与前提

细分项

注意与前提

费用

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

鉴权(API Key)

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

BaseURL

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

Daft 调用

算子参数

输入

输入列名

说明

image

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

image_name

可选参数,包含图像标识名的数组,用于生成输出文件名。

输出

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

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

参数

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

参数名称

类型

默认值

描述

image_suffix

str

.jpg

输出文件的后缀名。
描述: 保存到 TOS / 本地 时使用的文件后缀,需为支持的格式。
可选值: [".jpg", ".jpeg", ".png", ".webp"]
默认值: ".jpg"

output_dir

str

""

保存图像的输出文件夹路径。
描述: 将压缩后的图像保存到的输出路径,为空则不保存到输出,支持 TOS 和本地路径。
默认值: ""

image_src_type

str

image_url

输入图像的格式类型。
可选值: ["image_url", "image_base64", "image_binary"]
默认值: "image_url"

target_size

list

None

压缩后图像的尺寸。
描述: 格式为 [width, height];None 表示保持原尺寸。
默认值: None

quality

int

85

压缩质量参数(适配不同格式):

  • JPEG/WebP:1-100(数值越高质量越好)
  • PNG:映射为 compress_level(1→9,100→0)

默认值: 85

method

str

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_compress import ImageCompress
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_compress"

    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_ip_adapter"],
    }
    image_suffix = ".jpg"
    image_src_type = "image_url"
    target_size = [200, 200]
    quality = 85
    method = "lanczos"
    local_dir = ""
    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "image_compress",
        las_udf(
            ImageCompress,
            construct_args={
                "image_suffix": image_suffix,
                "output_dir": output_tos_dir,
                "image_src_type": image_src_type,
                "target_size": target_size,
                "quality": quality,
                "method": method,
            },
            batch_size=1,
        )(col("image"), col("image_name")),
    )
    ds.show()
# ╭────────────────────────────────┬────────────────┬────────────────────────────────────────────╮
# │ image                          ┆ image_name     ┆ image_compress                             │
# │ ---                            ┆ ---            ┆ ---                                        │
# │ String                         ┆ String         ┆ Struct[base64: String, image_path: String] │
# ╞════════════════════════════════╪════════════════╪════════════════════════════════════════════╡
# │ https://las-ai-qa-online.tos-… ┆ cat_ip_adapter ┆ {base64: /9j/4AAQSkZJRgABAQAA…             │
# ╰────────────────────────────────┴────────────────┴────────────────────────────────────────────╯
最近更新时间:2026.02.10 15:51:19
这个页面对您有帮助吗?
有用
有用
无用
无用