图像重采样处理器
更新时间:2026-04-23
简介
图像重采样算子用于对输入图像进行尺寸重采样(仅支持降采样)
功能描述
- 支持 4 种插值算法($nearest / bilinear / bicubic / lanczos$)与$ .jpg/.png $输出格式
- 输入支持本地/挂载目录/对象存储BOS 中原始数据输入
- 仅支持降采样:
target_size的宽高不得超过原图。 - 输入图像格式支持:JPEG/PNG/TIFF(JPG/TIF 作为别名支持)
- 输入图像大小限制:文件大小不超过 100MB;像素总数不超过 225,000,000
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 包含输入图像的数组,支持URL/BOS/base64/二进制格式 |
| images_name | 可选参数,包含图像标识名的数组,用于生成输出文件名。建议传入以确保文件名唯一性,且不要带后缀;如果不传入,会为无法解析出名称的图片随机生成名称。 |
输出
| 输出 | 含义 |
|---|---|
| base64 | 压缩后图像的base64编码 |
| image_path | 本地存储路径(当配置输出目录时有效) |
参数
| 参数 | 类型 | 含义 | 默认值 |
|---|---|---|---|
| image_suffix | str | 重采样的图像格式。可选值: [".jpg", ".png"] | '.jpg' |
| local_dir | str | 重采样后图像的本地存储目录.如果为空,则调整后的图像不保存到本地。 | "" |
| image_src_type | str | 输入图像的格式类型,支持格式:"image_url" (url地址), "image_base64" (base64编码), "image_binary" (二进制流) | "image_url", |
| target_size | list | 重采样后的图像尺寸。格式为 [width, height], | [200, 200], |
| target_dpi | list | 图像 DPI。格式为 [width, height] | [72, 72] |
| method | str | 重采样方法。支持方法:"nearest" (最近邻插值)、"bilinear" (双线性插值)、"bicubic" (双三次插值)、"lanczos" (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_resample import ImageResample
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_resample",
29 aihc_udf(
30 ImageResample,
31 construct_args={
32 "image_suffix": ".jpg",
33 "bos_dir": "",
34 "local_dir": "./test_images_output",
35 "image_src_type": "image_url",
36 "target_size": (200, 200),
37 "target_dpi": (72, 72),
38 "method": "lanczos",
39 },
40 num_gpus=0,
41 batch_size=1,
42 )(col("image"), col("image_name")),
43 )
44
45 ds.show()
评价此篇文章
