PsdImage 和
Layer 为核心,让 Python 开发者能够以编程方式访问 PSD 文档的结构,而无需
本地依赖进行编译。该库读取 PSD/PSB 文件的尺寸、颜色模式、位深度和压缩
方式,并通过 PsdImage.layers 暴露每个图层:每个 Layer 报告其名称,
边界、不透明度、可见性以及混合模式键(BlendMode),以及更底层的
通道信息(ChannelInformation)和蒙版数据(LayerMaskData)。可以对
部分图层元数据——名称、边界(left/top/right/bottom)和混合模式——进行
就地修改并使用 PsdImage.save() 持久化回磁盘;其余内容在此 FOSS 构建中设计为只读。Aspose.PSD FOSS for Python 采用 MIT 许可证。使用
pip install aspose-psd-foss — 该库没有任何第三方本地
依赖,仅依赖 Python 标准库。企业产品系列请参阅 Aspose.PSD — Enterprise Product Family。Image.load(file_path) 打开文件并返回一个 PsdImage 实例PsdImage.width、height 和 channels_count 报告画布尺寸和通道数PsdImage.layers 返回文档顺序中的完整 Layer 对象列表PsdImage.layers 返回文档顺序中的每个 Layer;每个 Layer 公开 name、bounds、opacity、is_visible 和 blend_mode_keyLayer.channel_information 公开用于更深入检查的通道元数据Layer.channels_count 报告层携带了多少图像通道PsdImage.layers 本身可以重新赋值,以删除或重新排序层Layer.name、Layer.left/top/right/bottom 和 Layer.blend_mode_keyPsdImage.save(file_path) 将修改后的文档持久化回磁盘PsdImage.load 重新加载已保存的文件,可确认编辑正确往返NotSupportedException —— 此构建仅支持元数据编辑,不支持完整的重新创作Layer.channel_information 暴露每个通道的元数据集合Layer.has_mask_data 和 has_blending_ranges_data 报告是否存在可选章节PsdLoadException 和 PsdSaveException 以清晰的消息显示加载/保存错误使用 Image.load 加载文件,以获取用于 PSD 特定成员的 PsdImage 实例。
from aspose_psd_foss.image import Image
with Image.load("test.psd") as image:
print(f"Width: {image.width}, Channels: {image.channels_count}, Layers: {len(image.layers)}")
图层的边界和混合模式可以就地更改并保存回磁盘。
from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.layers.blendmode import BlendMode
with PsdImage.load("test.psd") as image:
image.layers[0].left = 10
image.layers[0].top = 20
image.layers[0].right = 40
image.layers[0].bottom = 60
image.layers[0].blend_mode_key = BlendMode.MULTIPLY
image.save("edited.psd")
with PsdImage.load("edited.psd") as reloaded:
print(f"Bounds: {reloaded.layers[0].bounds}, BlendMode: {reloaded.layers[0].blend_mode_key}")
频道信息可以读取,但不能重新赋值——尝试设置它会引发 NotSupportedException。
from aspose_psd_foss.psdimage import PsdImage
with PsdImage.load("test.psd") as image:
layer = image.layers[0]
channel_information = layer.channel_information
print(f"Channel count: {layer.channels_count}, first channel: `NotSupportedException`")
# layer.channel_information = channel_information # raises NotSupportedException
Aspose.PSD FOSS for Python 是 MIT 许可证下发布的。您可以在 MIT 许可证的条款下使用、修改和重新分发它,包括在商业产品中。
请参阅上面的安装章节,以获取当前的安装命令。
它加载和保存 PSD 和 PSB(Photoshop 大型文档)文件。它不渲染或导出像素数据到其他栅格格式,如 PNG 或 JPEG。
PsdImage(Image的子类)。使用 PsdImage.load 加载文档,然后读取其 layers 集合和头部属性。
是的 — Layer 为多个属性(如可见性和名称)公开了可用的 setter,PsdImage.layers 本身也提供了用于替换集合的 setter。少数特定属性(通道信息、蒙版数据、混合范围以及一些文档级资源)在此 FOSS 构建中为只读,若尝试设置会抛出 NotSupportedException。