Last active
August 7, 2025 11:36
-
-
Save aspose-com-gists/55172fdd21b555665ace37e1a6d19cd0 to your computer and use it in GitHub Desktop.
Crop, Resize PSD Layers in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import required classes from Aspose.PSD | |
import aspose.psd as psd | |
from aspose.psd import Image, ResizeType, Color, Rectangle | |
from aspose.psd.fileformats.png import PngColorType | |
from aspose.psd.fileformats.psd import PsdImage | |
from aspose.psd.imageloadoptions import PsdLoadOptions | |
from aspose.psd.imageoptions import PngOptions | |
from aspose.pycore import cast | |
# Define input PSD file and output PNG file paths. | |
source = "layers.psd" | |
output_original = "original_layer_manipulation.png" | |
output_updated = "updated_layer_manipulation.png" | |
# Create an object of the PngOptions class and set PNG save options with alpha channel. | |
pngOpt = PngOptions() | |
pngOpt.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
# Initialize an instance of the PsdLoadOptions class and set PSD load options. | |
psdLoadOpt = PsdLoadOptions() | |
psdLoadOpt.load_effects_resource = True | |
psdLoadOpt.allow_warp_repaint = True | |
# Load the PSD image using the specified options. | |
with Image.load(source, psdLoadOpt) as image: | |
# Cast the loaded image to PsdImage type for layer access. | |
psd_image = cast(PsdImage, image) | |
# Save the original PSD (before any changes) as a PNG for comparison. | |
psd_image.save(output_original, pngOpt) | |
# Invoke the resize method to resize layer at index 2 to 25x25 pixels using high-quality resampling. | |
psd_image.layers[2].resize(25, 25, ResizeType.HIGH_QUALITY_RESAMPLE) | |
# Rotate layer at index 5 by 45 degrees clockwise with yellow background fill. | |
psd_image.layers[5].rotate(45, True, Color.yellow) | |
# Increase contrast of layer at index 3 by a factor of 3. | |
psd_image.layers[3].adjust_contrast(3) | |
# Crop layer at index 10 to a rectangle starting at (10, 10) with size 20x20. | |
psd_image.layers[10].crop(Rectangle(10, 10, 20, 20)) | |
# Call the save method to save the updated image after all layer modifications. | |
psd_image.save(output_updated, pngOpt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment