Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created October 13, 2025 20:52
Show Gist options
  • Save aspose-com-gists/d9f4f3a65612d54a55b8440b071a4d4a to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d9f4f3a65612d54a55b8440b071a4d4a to your computer and use it in GitHub Desktop.
Insert Image in Photoshop Layer using Python
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