图像裁剪处理器
更新时间:2026-04-23
简介
图像裁剪处理器
功能描述
-
输入格式:
- URL 地址(image_url)
- BOS路径 ("bos://{bucket}/{path}")
- Base64 编码(image_base64)
- 二进制流(image_binary)
-
裁剪方式(支持多种算法/模式):
- 坐标裁剪:指定左上角/右下角坐标精准裁剪
- 比例裁剪:按宽高比例裁剪(0-1)
- 中心裁剪:按指定尺寸从图像中心裁剪
- 插值算法:nearest / bilinear / bicubic / lanczos(裁剪后尺寸适配时使用)
-
输出模式:
- 返回裁剪后图片的 Base64 字符串
- 可选将图片保存到本地目录
-
格式适配:
- JPEG:RGBA 转 RGB + 质量压缩
- PNG:保留透明通道 + 无损压缩
- WebP:支持透明通道 + 质量/无损压缩
算子参数
输入
| 输入 | 含义 |
|---|---|
| image | 包含输入图像的数组,支持URL/base64/二进制格式 |
| image_name | 可选参数,包含图像标识名的数组,用于生成输出文件名 |
输出
| 输出 | 含义 |
|---|---|
| base64 | 压缩后图像的base64编码 |
| image_path | 本地存储路径(当配置输出目录时有效) |
参数
| 参数 | 类型 | 含义 | 默认值 |
|---|---|---|---|
| image_suffix | str | 保存到本地时使用的文件后缀,需为支持的格式。可选值: [".jpg", ".jpeg", ".png", ".webp"] | ".jpg" |
| output_dir | str | 将裁剪后的图像保存到的输出路径,为空则不保存到输出, | "" |
| image_src_type | str | 输入图像的格式类型。可选值: ["image_url", "image_base64", "image_binary"] | "image_url" |
| crop_type | str | 裁剪方式。支持三种裁剪模式,需配合对应参数使用:coordinate: 需指定 crop_coords=[x1, y1, x2, y2]ratio: 需指定 crop_ratio=[width_ratio, height_ratio](0-1)center: 需指定 crop_size=[width, height](或使用默认比例)可选值: ["coordinate", "ratio", "center"] | "center" |
| crop_coords | list | 坐标裁剪参数。格式:[x1, y1, x2, y2],x1/y1=左上角坐标,x2/y2=右下角坐标。 | None |
| crop_ratio | list | 比例裁剪参数。格式:[width_ratio, height_ratio],值范围 0-1(如 [0.8, 0.8] 裁剪80%区域)。 | [0.8, 0.8] |
| crop_size | list | 中心裁剪参数。格式: [width, height],指定裁剪后的宽高,None 则按原尺寸80%裁剪。 | None |
| quality | int | 保存质量参数(适配不同格式):JPEG/WebP:1-100(数值越高质量越好)PNG:映射为 compress_level(1→9,100→0) | 85 |
| method | str | 插值算法(裁剪后尺寸适配时使用)。可选值: ["nearest", "bilinear", "bicubic", "lanczos"] | "lanczos" |
调用示例
Plain Text
1from __future__ import annotations
2
3import os
4import daft
5from daft import col
6
7from daft.aihc.common.udf import aihc_udf
8from daft.aihc.functions.image.image_crop import ImageCrop
9
10if __name__ == "__main__":
11 if os.getenv("DAFT_RUNNER", "native") == "ray":
12 import ray
13 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
14 daft.set_runner_ray()
15 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
16
17 samples = {
18 "image": [
19 "file:///local/sample_1.jpg",
20 "file:///mnt/pfs/sample_2.jpg",
21 "file:///mnt/bos/sample_3.jpg",
22 ],
23 "image_name": ["sample_1.jpg", "sample_2.jpg", "sample_3.jpg"],
24 }
25
26 ds = daft.from_pydict(samples)
27 ds = ds.with_column(
28 "image_crop",
29 aihc_udf(
30 ImageCrop,
31 construct_args={
32 "image_suffix": ".jpg",
33 "output_dir": "./test_images_output",
34 "image_src_type": "image_url",
35 "crop_type": "center",
36 "crop_size": [500, 500],
37 "crop_ratio": [0.8, 0.8],
38 "crop_coords": [100, 100, 800, 800],
39 "quality": 85,
40 "method": "lanczos",
41 },
42 batch_size=1,
43 )(col("image"), col("image_name")),
44 )
45 ds.show()
评价此篇文章
