图片
图片类型内部使用Pillow库处理图片,获取PIL Image对象后通过PIL库处理图片。 支持处理的图片格式:JPEG、PNG。
| 属性名 | 类型 | 说明 |
|---|---|---|
| filename | str | 图像的文件名或路径(如果从文件加载)。 |
| format | str | 图像格式(如PNG、JPEG)。 |
| height | int | 图像高度(像素)。 |
| width | int | 图像宽度(像素)。 |
| data | bytes | 图像数据。 |
| 方法名 | 参数 | 返回值 | 说明 |
|---|---|---|---|
| __init__ | filename: Union[str, os.PathLike] = None data: Union[str, PIL.Image] = None format: Optional[str] = None | None | 构造方法:从文件(filename)或数据(data)加载图像,支持bytes或Pillow对象。 |
| to_numpy | - | np.ndarray | 将图像转换为NumPy数组(float32类型)。 |
| resize | new_shape: tuple[int, int] | Image | 返回调整尺寸后的新图像。 |
| show | - | None | 调用Pillow的默认图像查看器显示图像。 |
| to_thumbnail | size: tuple[int, int] = (100, 100) | Image | 生成指定尺寸的缩略图。 |
| crop | box: tuple[float, float, float, float] | Image | 裁剪图像指定区域,返回新的 Image 对象 |
| getbands | - | tuple: [str, ...] | 调用Pillow的getbands() |
| getmode | - | str | 返回图像mode |
| to_struct | - | dict | 返回图片结构化数据 |
图片读写
该示例演示如何从OBS路径下“obs://xxxxx/xxxxx”读取图片,并且加载到Dataset中,然后从中随机选取3张图片再写回到指定的OBS路径下。
import os
from aura_frame.multimodal import ai_lake
target_database = "test"
conn = ai_lake.connect(
aura_endpoint=os.getenv("AURA_ENDPOINT"),
aura_endpoint_name=os.getenv("AURA_ENDPOINT_NAME"),
aura_workspace_id=os.getenv("AURA_WORKSPACE"),
lf_catalog_name=os.getenv("AURA_CATALOG"),
access_key=os.getenv("access_key"),
secret_key=os.getenv("secret_key"),
default_database=target_database,
use_single_cn_mode=True
)
ds = conn.read_images("obs://xxxxx/xxxxx", file_extensions=["png", "jpg"], size=(300, 300), mode="RGB", include_paths=True)
ds.show(3)
ds_to_write = ds.select_columns([ds.image]).limit(3)
ds_to_write.write_images("obs://xxxxx/xxxxx", "image", file_format="jpeg", filename_prefix="image_", try_create_dir=True)
conn.close()