How to Load a PSD Document in Python

How to Load a PSD Document in Python

PsdImage.load is the single public entry point for opening a PSD/PSB document — it accepts either a file path or an already-open readable stream. The low-level section-by-section reader it delegates to internally is not something you call directly.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-Python.git
cd Aspose.PSD-FOSS-for-Python
# No packaging manifest is published yet -- run from this folder,
# or add it to PYTHONPATH:
export PYTHONPATH=$PWD:$PYTHONPATH

Step 2: Import Required Classes

from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.coreexceptions.psdloadexception import PsdLoadException

Step 3: Load from a File Path

image = PsdImage.load("input.psd")
print(f"Loaded: {image.width}x{image.height}")
image.dispose()

Step 4: Load from a Stream

A stream doesn’t need to be seekable — load reads its full contents into an internal buffer before parsing:

with open("input.psd", "rb") as stream:
    image = PsdImage.load(stream)

print(f"Loaded from stream: {image.width}x{image.height}")
image.dispose()

Common Issues and Fixes

FileNotFoundError: File not found: .... The path passed to load doesn’t point to an existing file — load checks os.path.isfile before attempting to open it.

PsdLoadException: Unexpected end of PSD/PSB data while reading the file structure. The source stream ran out of data mid-parse (an EOFError while reading) — the file is likely truncated.

PsdLoadException: Failed to read PSD/PSB data from the source stream. An I/O error occurred while reading from the stream itself, wrapped and re-raised as a PsdLoadException.

ArgumentNullException when calling load. load was called with None as the stream argument.

Frequently Asked Questions

Do I need to seek a stream back to its start before calling load?

No — if the stream is seekable, load restores its original position after reading; if it isn’t seekable, load still works since it buffers the stream’s contents internally before parsing.

What’s the difference between the two FileNotFoundError/PsdLoadException failures above?

A missing file path raises the standard FileNotFoundError before any parsing starts. A PsdLoadException instead means a file (or stream) was found and opened, but the PSD/PSB data itself is truncated, corrupted, or otherwise unreadable once parsing began — see also Troubleshooting for the version- and boundary-related PsdLoadException variants.

See Also