Layers

Layers

PsdImage.layers returns every parsed layer as a list of Layer objects. Each Layer exposes its own geometry, visibility and compositing state, and metadata about its channels, mask, and blending ranges.


Reading Layer Metadata

name, bounds (and the derived width/height/top/left/bottom/right), is_visible, opacity, clipping, blend_mode_key, and channels_count are all readable directly.

from aspose_psd_foss.psdimage import PsdImage

with PsdImage.load("input.psd") as image:
    for layer in image.layers:
        print(f"{layer.name}: {layer.bounds}, "
              f"Visible={layer.is_visible}, Opacity={layer.opacity}, "
              f"BlendMode={layer.blend_mode_key}")

Supported Metadata Edits

name, is_visible, opacity, clipping, blend_mode_key, and the four bounds edges (top/left/bottom/right) can be changed in place and written back with PsdImage.save().

from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.layers.blendmode import BlendMode

with PsdImage.load("input.psd") as image:
    layer = image.layers[0]
    layer.name = f"{layer.name} (edited)"
    layer.is_visible = not layer.is_visible
    layer.opacity = 128
    layer.blend_mode_key = BlendMode.MULTIPLY
    image.save("output.psd")

Channel and Mask Metadata

channel_information, layer_mask_data, and layer_blending_ranges_data are read-only in this FOSS build — their setters raise NotSupportedException.

layer = image.layers[0]
for channel in layer.channel_information:
    print(f"Channel {channel.ChannelID}: {channel.compression_method}, {channel.length} bytes")

if layer.has_mask_data:
    mask = layer.layer_mask_data
    print(f"Mask bounds: {mask.mask_rectangle}, flags: {mask.flags}")

Removing or Reordering Layers

PsdImage.layers has a setter as well as a getter: assigning a filtered or reordered list replaces the document’s layer collection.

from aspose_psd_foss.psdimage import PsdImage

with PsdImage.load("input.psd") as image:
    image.layers = [layer for layer in image.layers if layer.is_visible]
    image.save("output.psd")

Tips and Best Practices

  • Index into image.layers by position; the list order matches the document’s own layer order.
  • blend_mode_key accepts any BlendMode enum value — no validation is performed against the document’s declared color mode.
  • channel_information, layer_mask_data, and layer_blending_ranges_data can be read but never reassigned.
  • Call PsdImage.save() once after all edits; each call writes the full document.

Common Issues

IssueCauseFix
NotSupportedException when setting channel_information, layer_mask_data, or layer_blending_ranges_dataThese 3 properties are read-only in the FOSS build despite having a setterRead the value; do not attempt to reassign it
layer_mask_data access when there is no maskNot every layer has a mask subsectionCheck layer.has_mask_data before reading layer.layer_mask_data

FAQ

Can I remove a layer?

Yes — reassign image.layers to a filtered list.

Can I change a layer’s channel or mask data?

No. channel_information, layer_mask_data, and layer_blending_ranges_data are read-only — only name, is_visible, opacity, clipping, blend_mode_key, and the bounds edges can be changed.


API Reference Summary

Class/MethodDescription
PsdImage.layersGets or sets the layer collection — settable for removal/reordering
Layer.name / is_visible / opacity / clipping / blend_mode_key / top / left / bottom / rightSupported layer metadata edits
Layer.bounds / width / height / channels_countRead-only layer geometry
Layer.channel_information / layer_mask_data / layer_blending_ranges_dataRead-only channel/mask/blending metadata

See Also