Aspose.PSD Add Layer From File
Overview
Adding layers to images is a common task in image editing and manipulation. With Aspose.PSD for Python, you can easily add any file as a layer to your PSD (Photoshop Document) files using a simple and straightforward API.
Aspose.PSD for Python provides a powerful set of features for working with PSD files, including layer editing capabilities. The library supports a wide range of image formats, allowing you to add layers from various file types such as JPEG, PNG, TIFF, and more.
To add an image file as a layer, you can use the ‘add_layer’ method provided by the Aspose.PSD API. This method accepts the path to the image file as input and automatically converts it into a layer that can be added to your PSD file.
Example
Here’s a code sample that demonstrates how to add an image file as a layer using Aspose.PSD for Python:
# This example from Aspose.PSD for Python Gist | |
from io import BytesIO | |
from aspose.psd.fileformats.psd import PsdImage | |
from aspose.psd.fileformats.psd.layers import Layer | |
inputFile = "inputFile.png" | |
outputFile = "AddFileAsLayer.psd" | |
# Open file as Stream | |
with open(inputFile, "rb", buffering=0) as filestream: | |
stream = BytesIO(filestream.read()) | |
stream.seek(0) | |
# Create PSD Layer from Stream | |
layer = Layer(stream) | |
# Create PSD Image with the specified size | |
psdImage = PsdImage(layer.width, layer.height) | |
# Add Layer to PSD Image | |
psdImage.layers = [layer] | |
# Get Pixels from File | |
pixels = layer.load_argb_32_pixels(layer.bounds) | |
pixelsRange = range(len(pixels)) | |
# Fill the pixels data with some values | |
for i in pixelsRange: | |
if i % 5 == 0: | |
pixels[i] = 500000 | |
# Fast Save of Updated Image Data | |
layer.save_argb_32_pixels(layer.bounds, pixels) | |
# Save PSD Image | |
psdImage.save(outputFile) |