How to Work with PSD Layers in Python

How to Work with PSD Layers in Python

A loaded PsdImage’s layers property returns its collection of Layer objects. Each Layer exposes editable metadata — name, visibility, opacity, clipping, blend mode, and position (bounds edges) — plus read-only overall bounds and channel information.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-Python.git
cd Aspose.PSD-FOSS-for-Python
# No packaging manifest is published yet -- run from this folder,
# or add it to PYTHONPATH:
export PYTHONPATH=$PWD:$PYTHONPATH

Step 2: Import Required Classes

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

Step 3: Load a Document and List Its Layers

image = PsdImage.load("input.psd")

for layer in image.layers:
    print(f"{layer.name}: {layer.width}x{layer.height} at ({layer.left},{layer.top})")

Step 4: Read and Edit Layer Visibility, Opacity, and Blend Mode

is_visible, opacity (conventionally 0-255), clipping, name, and blend_mode_key all have working setters:

layer = image.layers[0]

layer.is_visible = False          # hide the layer
layer.opacity = 200               # ~78% opacity
layer.blend_mode_key = BlendMode.MULTIPLY
layer.name = "Renamed Layer"

Step 5: Read and Reposition Layer Bounds

bounds, width, height, and channels_count are read-only — but top, left, bottom, and right each have a working setter that repositions the layer:

print(f"Bounds: {layer.bounds}")
print(f"Channels: {layer.channels_count}")

layer.left = 10
layer.top = 20
layer.right = 40
layer.bottom = 60

Step 6: Replace the Layers Collection

PsdImage.layers itself has a working setter, so you can replace the whole collection (for example, to remove a layer):

image.layers = [l for l in image.layers if l.name != "Renamed Layer"]

Step 7: Save the Document

image.save("output.psd")
image.dispose()

Common Issues and Fixes

NotSupportedException when setting channel_information, layer_mask_data, or layer_blending_ranges_data. These three Layer setters are not supported in this FOSS build — treat them as read-only.

Opacity value looks wrong after setting it. opacity is conventionally a 0-255 value, not a 0.0-1.0 fraction or a percentage — layer.opacity = 200 means roughly 78% opaque (200/255).

Changes to a layer don’t appear after saving. Confirm you are modifying the Layer instance obtained from image.layers, and that you call image.save() after making your edits.

Frequently Asked Questions

Can I remove a layer from a document?

Yes — PsdImage.layers has a working setter, so you can assign it a filtered list to remove layers (see Step 6).

What blend modes are available?

BlendMode includes values such as NORMAL, MULTIPLY, SCREEN, OVERLAY, and DARKEN, among others — set via Layer.blend_mode_key.

Is layer mask data available?

Layer.layer_mask_data has a working getter, but its setter raises NotSupportedException in this FOSS build.

See Also