Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 6, 2025 13:36
Show Gist options
  • Save aspose-com-gists/480135cb91504e331dec5e408f3c0a74 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/480135cb91504e331dec5e408f3c0a74 to your computer and use it in GitHub Desktop.
Working with Photoshop Layers in Python
# Import necessary modules from Aspose.PSD for image processing
import aspose.psd as psd
from aspose.psd import Color
from aspose.psd.fileformats.core.blending import BlendMode
from aspose.psd.imageoptions import PngOptions
from aspose.pycore import cast
from aspose.psd.fileformats.png import PngColorType
from aspose.psd.fileformats.psd import PsdImage
# Define file paths
source = "layers.psd"
output_original = "original.png"
output_updated = "output_updated.png"
# Load the PSD file using Aspose.PSD
with PsdImage.load(source) as image:
psdImage = cast(PsdImage, image)
# Set PNG save options to include alpha (transparency)
pngSaveOpt = PngOptions()
pngSaveOpt.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
# Save the original unmodified PSD as a PNG
image.save(output_original, pngSaveOpt)
# Modify layer properties:
# Set opacity of the 2nd layer (index 1) to 100%
psdImage.layers[1].opacity = 100
# Set the blending mode of the 5th layer (index 4) to "Hue"
psdImage.layers[4].blend_mode_key = BlendMode.HUE
# Add a drop shadow effect to the 8th layer (index 7)
shadow = psdImage.layers[7].blending_options.add_drop_shadow()
shadow.angle = 30
shadow.color = Color.from_argb(255, 255, 0)
# Change the blending mode of the 10th layer (index 9) to "Lighten"
psdImage.layers[9].blend_mode_key = BlendMode.LIGHTEN
# Add a color overlay effect to the 6th layer (index 5)
colorOverlay = psdImage.layers[5].blending_options.add_color_overlay()
colorOverlay.color = Color.from_argb(200, 30, 50)
colorOverlay.opacity = 150
# Save the modified image as a PNG.
image.save(output_updated, pngSaveOpt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment