psd_tools

See Usage for examples.

PSDImage

class psd_tools.PSDImage(data)[source]

Photoshop PSD/PSB file object.

Example:

from psd_tools import PSDImage

psd = PSDImage.open('example.psd')
image = psd.topil()

for layer in psd:
    if layer.has_pixels():
        layer_image = layer.topil()
bbox

Minimal bounding box that contains all the visible layers.

Use viewbox to get viewport bounding box. When the psd is empty, bbox is equal to the canvas bounding box.

Returns:(left, top, right, bottom) tuple.
bottom

Bottom coordinate.

Returns:int
channels

Number of color channels.

Returns:int
color_mode

Document color mode, such as ‘RGB’ or ‘GRAYSCALE’. See ColorMode.

Returns:str
compose(force=False, bbox=None, **kwargs)[source]

Compose the PSD image.

See compose() for available extra arguments.

Parameters:bbox – Viewport tuple (left, top, right, bottom).
Returns:PIL.Image, or None if there is no pixel.
depth

Pixel depth bits.

Returns:int
descendants(include_clip=True)

Return a generator to iterate over all descendant layers.

Example:

# Iterate over all layers
for layer in psd.descendants():
    print(layer)

# Iterate over all layers in reverse order
for layer in reversed(list(psd.descendants())):
    print(layer)
Parameters:include_clip – include clipping layers.
classmethod frompil(image, compression=<Compression.PACK_BITS: 1>)[source]

Create a new PSD document from PIL Image.

Parameters:
  • image – PIL Image object.
  • compression – ImageData compression option. See Compression.
Returns:

A PSDImage object.

has_preview()[source]

Returns if the document has real merged data. When True, topil() returns pre-composed data.

has_thumbnail()[source]

True if the PSDImage has a thumbnail resource.

height

Document height.

Returns:int
image_resources

Document image resources.

Returns:ImageResouces.
is_group()[source]

Return True if the layer is a group.

Returns:bool
is_visible()[source]

Returns visibility of the element.

Returns:bool
kind

Kind.

Returns:‘psdimage’
left

Left coordinate.

Returns:0
name

Element name.

Returns:‘Root’
classmethod new(mode, size, color=0, depth=8, **kwargs)[source]

Create a new PSD document.

Parameters:
  • mode – The color mode to use for the new image.
  • size – A tuple containing (width, height) in pixels.
  • color – What color to use for the image. Default is black.
Returns:

A PSDImage object.

offset

(left, top) tuple.

Returns:tuple
classmethod open(fp)[source]

Open a PSD document.

Parameters:fp – filename or file-like object.
Returns:A PSDImage object.
parent

Parent of this layer.

right

Right coordinate.

Returns:int
save(fp, mode='wb')[source]

Save the PSD file.

Parameters:
  • fp – filename or file-like object.
  • mode – file open mode, default ‘wb’.
size

(width, height) tuple.

Returns:tuple
tagged_blocks

Document tagged blocks.

Returns:TaggedBlocks or None.
thumbnail()[source]

Returns a thumbnail image in PIL.Image. When the file does not contain an embedded thumbnail image, returns None.

top

Top coordinate.

Returns:0
topil()[source]

Get PIL Image.

Returns:PIL.Image, or None if the composed image is not available.
version

Document version. PSD file is 1, and PSB file is 2.

Returns:int
viewbox

Return bounding box of the viewport.

Returns:(left, top, right, bottom) tuple.
visible

Visibility.

Returns:True
width

Document width.

Returns:int

compose

psd_tools.compose(layers, bbox=None, layer_filter=None, color=None)[source]

Compose layers to a single PIL.Image. If the layers do not have visible pixels, the function returns None.

Example:

image = compose([layer1, layer2])

In order to skip some layers, pass layer_filter function which should take layer as an argument and return True to keep the layer or return False to skip:

image = compose(
    layers,
    layer_filter=lambda x: x.is_visible() and x.kind == 'type'
)

By default, visible layers are composed.

Note

This function is experimental and does not guarantee Photoshop-quality rendering.

Currently the following are ignored:

  • Adjustments layers
  • Layer effects
  • Blending mode (all blending modes become normal)

Shape drawing is inaccurate if the PSD file is not saved with maximum compatibility.

Parameters:
  • layers – a layer, or an iterable of layers.
  • bbox – (left, top, bottom, right) tuple that specifies a region to compose. By default, all the visible area is composed. The origin is at the top-left corner of the PSD document.
  • layer_filter – a callable that takes a layer and returns bool.
  • color – background color in int or tuple.
Returns:

PIL.Image or None.