Created
October 13, 2025 20:52
-
-
Save aspose-com-gists/d9f4f3a65612d54a55b8440b071a4d4a to your computer and use it in GitHub Desktop.
Insert Image in Photoshop Layer using 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
from io import BytesIO | |
import aspose.psd as psd | |
from aspose.psd.fileformats.psd import PsdImage | |
from aspose.psd.fileformats.psd.layers import Layer | |
inputFile = "sample.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 by initializing an instance of the Layer class. | |
layer = Layer(stream) | |
# Instantiate an object of the PsdImage class to 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 by calling the load_argb_32_pixels method. | |
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 | |
layer.save_argb_32_pixels(layer.bounds, pixels) | |
# Save PSD Image by calling the save method. | |
psdImage.save(outputFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment