Resources

Resources

已加载的 PsdImage 通过两个只读视图公开其图像资源块——元数据式条目,如网格、参考线和 ICC 配置文件负载——以及针对索引颜色调色板的单独摘要。


列出资源块

PsdImage.image_resources 为每个资源返回一个 PreservedResourceBlock,包括原始资源 ID、名称和字节大小:

from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.resources.imageresourceids import ImageResourceIds

image = PsdImage.load(input_path)

for block in image.image_resources:
    print(f"ID: {block.id}, name: {block.name!r}, size: {block.size} bytes")

PsdImage.resources 改为为每个资源返回单独的 PsdResourceInfo 摘要,并添加一个 kind 分类——在此 FOSS 构建中,kind 始终为 PsdResourceKind.UNKNOWN,因为此构建的资源解析器不会将特定资源 ID 解码为语义属性。


识别已知的资源 ID

由于 kind 未被语义解析,可通过将 block.id 与 ImageResourceIds 中的常量进行比较来识别特定资源:

for block in image.image_resources:
    if block.id == ImageResourceIds.ICC_PROFILE:
        print(f"Found an ICC profile resource ({block.data_size} bytes)")

读取索引颜色调色板

如果文档的颜色数据基于调色板,PsdImage.indexed_palette 将返回其条目:

palette = image.indexed_palette

if palette is not None:
    print(f"Palette entries: {len(palette.entries)}")

技巧与最佳实践

  • PsdImage.image_resources 的 getter 可用于列出资源,但其 setter 会抛出 NotSupportedException —— 在此 FOSS 构建中资源是只读的。
  • PsdImage.has_icc_profile、is_icc_profile_untagged 和 global_angle 在此 FOSS 构建中是固定的占位符值(分别始终为 False、None 和 0),不受加载的文档实际内容的影响 —— 如果需要自行检测这些资源,请使用上述资源 ID 比较模式。
  • 如果直接调用 PreservedResourceBlock.save(),它会抛出 NotImplementedError —— 当你对整个文档调用 image.save() 时,资源会自动往返。

常见问题

问题原因修复
image.indexed_palette 是 None文档的颜色数据类型不是 INDEXED_PALETTE仅索引颜色文档包含调色板
NotSupportedException 设置 image.image_resources此 FOSS 构建不支持设置器将 image_resources 视为只读
has_icc_profile / is_icc_profile_untagged / global_angle 永不更改这三个属性在此 FOSS 构建中是硬编码的占位符存根请改为将 block.id 与 ImageResourceIds 进行比较(见上文)

FAQ

什么是 image_resources 和 resources 之间的区别?

两者列出相同的底层资源块,但返回不同的对象类型:image_resources 返回 PreservedResourceBlock 对象(原始 ID/名称/大小,用于往返),而 resources 返回 PsdResourceInfo 摘要(添加了 kind 分类,目前始终为 UNKNOWN)。

我可以修改或删除图像资源吗?

不——image_resources 的 setter 会抛出 NotSupportedException,如果直接调用,PreservedResourceBlock.save() 会抛出 NotImplementedError。在此 FOSS 构建中,资源是只读的。


API Reference 摘要

类/方法描述
PsdImage.image_resources原始资源块(PreservedResourceBlock,只读)
PsdImage.resources资源摘要(PsdResourceInfo,kind 总是 UNKNOWN)
PsdImage.indexed_palette索引颜色调色板摘要,若存在
ImageResourceIds已知的资源 ID 常量,用于与 resource_id/id 进行比较

另请参阅

 中文