图像宽高
更新时间:2026-09-14
简介
图像宽高算子:读取图像的原始像素宽高,以一个 struct(width + height)输出,逐行产出,按尺寸过滤交给 pipeline。
功能描述
- 只用 PIL
Image.open读图像头部取image.size,不做全图解码;纯 CPU,无模型权重 - 支持三种输入形式:
image_url(本地路径 /file:/// http(s) /bos:///s3://,远端路径先下载到临时目录)、image_base64(允许带 data URI 前缀)、image_binary - 宽高写在同一个 struct 里,下游用
col("shape")["width"]/col("shape")["height"]取字段 - 取文件记录的原始像素尺寸,不做 EXIF 旋转矫正
- 单行失败(读不到文件、非图像数据、输入类型与
image_src_type不匹配)打 exception 日志后整条 struct 填 None,不中断整批;不会出现 width 有值而 height 为 None 的半残行 - 一行一张图,逐行产出该图的宽高
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 图像输入列,内容形式由 image_src_type 决定(路径/URL 字符串、base64 字符串、bytes) |
输出
| 输出 | 含义 |
|---|---|
| shape | struct,处理失败时整条为 None |
| shape.width | int32,图像宽(像素) |
| shape.height | int32,图像高(像素) |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| image_src_type | str | "image_url" | 输入图像编码形式:image_url / image_base64 / image_binary,取值非法在初始化阶段抛 ValueError |
注意事项
- 本算子只产出统计字段,不做过滤。按宽高的区间筛选(如
width >= 1、height >= 1,上界不限)要自己在 pipeline 里写。 - 输出是静态 schema 的 struct,字段固定为
width+height,不要指望后续版本往里加字段。
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.image.image_shape import ImageShape
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18samples = {"image": ["bos://your-bucket/sample.jpg"]}
19 ds = daft.from_pydict(samples)
20 ds = ds.with_column(
21 "shape",
22 aihc_udf(
23 ImageShape,
24 construct_args={"image_src_type": "image_url"},
25 num_cpus=1,
26 concurrency=4,
27 batch_size=64,
28 )(col("image")),
29 )
30 ds = ds.with_column("width", col("shape")["width"])
31 ds = ds.with_column("height", col("shape")["height"])
32 ds.show()
评价此篇文章
