psd-tools

1.15.0.post1 · active · verified Thu Apr 16

psd-tools is a Python package designed for working with Adobe Photoshop PSD and PSB files, enabling reading, writing, and manipulation of layer data and compositions. The current version is 1.15.0.post1, and it maintains an active release cadence with frequent updates and improvements. It provides functionalities for low-level file structure access, raw layer image export, and limited layer composition.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a PSD file, composite the entire image, and then iterate through its layers, saving each visible pixel layer as a separate PNG. Ensure you have an 'example.psd' file available for the code to run successfully.

import os
from psd_tools import PSDImage

# Create a dummy PSD for demonstration if it doesn't exist
dummy_psd_path = 'example.psd'
if not os.path.exists(dummy_psd_path):
    # In a real scenario, you'd have an actual PSD file
    # For this example, we'll just demonstrate the API calls.
    # Creating a valid PSD requires more complex operations
    # than a simple placeholder here.
    print(f"Please create a dummy '{dummy_psd_path}' file to run this quickstart.")
    print("Example: a small Photoshop file with a few layers.")
else:
    psd = PSDImage.open(dummy_psd_path)

    # Print basic info
    print(f"Opened PSD: {psd}")

    # Composite the entire PSD and save as PNG
    composite_image = psd.composite()
    composite_image.save('example.png')
    print(f"Saved composite image to example.png")

    # Iterate through layers and save each as a separate PNG
    for i, layer in enumerate(psd):
        print(f"Layer {i}: {layer.name} (Type: {layer.kind})")
        if layer.is_pixel_layer() and layer.visible:
            layer_image = layer.composite()
            if layer_image:
                layer_image.save(f'{layer.name.replace(" ", "_")}.png')
                print(f"Saved layer '{layer.name}' to {layer.name.replace(" ", "_")}.png")

view raw JSON →