psd_tools.api.layers

Layer module.

Artboard

class psd_tools.api.layers.Artboard(*args)[source]

Artboard is a special kind of group that has a pre-defined viewbox.

Example:

artboard = psd[1]
image = artboard.compose()
property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(bbox=None, **kwargs)[source]

Compose the artboard.

See compose() for available extra arguments.

Parameters:

bbox – Viewport tuple (left, top, right, bottom).

Returns:

PIL.Image, or None if there is no pixel.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

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.

property effects

Layer effects.

Returns:

Effects

static extract_bbox(layers, include_invisible=False)

Returns a bounding box for layers or (0, 0, 0, 0) if the layers have no bounding box.

Parameters:

include_invisible – include invisible layers in calculation.

Returns:

tuple of four int

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property width

Width of the layer.

Returns:

int

Group

class psd_tools.api.layers.Group(*args)[source]

Group of layers.

Example:

group = psd[1]
for layer in group:
    if layer.kind == 'pixel':
        print(layer.name)
property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(force=False, bbox=None, layer_filter=None, context=None, color=None)

Compose layer and masks (mask, vector mask, and clipping layers).

Returns:

PIL Image object, or None if the layer has no pixels.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)[source]

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

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.

property effects

Layer effects.

Returns:

Effects

static extract_bbox(layers, include_invisible=False)[source]

Returns a bounding box for layers or (0, 0, 0, 0) if the layers have no bounding box.

Parameters:

include_invisible – include invisible layers in calculation.

Returns:

tuple of four int

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property width

Width of the layer.

Returns:

int

PixelLayer

class psd_tools.api.layers.PixelLayer(psd, record, channels, parent)[source]

Layer that has rasterized image in pixels.

Example:

assert layer.kind == 'pixel':
image = layer.topil()
image.save('layer.png')

composed_image = layer.compose()
composed_image.save('composed-layer.png')
property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(force=False, bbox=None, layer_filter=None)

Deprecated, use composite().

Compose layer and masks (mask, vector mask, and clipping layers).

Note that the resulting image size is not necessarily equal to the layer size due to different mask dimensions. The offset of the composed image is stored at .info[‘offset’] attribute of PIL.Image.

Parameters:

bbox – Viewport bounding box specified by (x1, y1, x2, y2) tuple.

Returns:

PIL.Image, or None if the layer has no pixel.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

property effects

Layer effects.

Returns:

Effects

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property width

Width of the layer.

Returns:

int

ShapeLayer

class psd_tools.api.layers.ShapeLayer(psd, record, channels, parent)[source]

Layer that has drawing in vector mask.

property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(force=False, bbox=None, layer_filter=None)

Deprecated, use composite().

Compose layer and masks (mask, vector mask, and clipping layers).

Note that the resulting image size is not necessarily equal to the layer size due to different mask dimensions. The offset of the composed image is stored at .info[‘offset’] attribute of PIL.Image.

Parameters:

bbox – Viewport bounding box specified by (x1, y1, x2, y2) tuple.

Returns:

PIL.Image, or None if the layer has no pixel.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

property effects

Layer effects.

Returns:

Effects

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property width

Width of the layer.

Returns:

int

SmartObjectLayer

class psd_tools.api.layers.SmartObjectLayer(psd, record, channels, parent)[source]

Layer that inserts external data.

Use smart_object attribute to get the external data. See SmartObject.

Example:

import io
if layer.smart_object.filetype == 'jpg':
    image = Image.open(io.BytesIO(layer.smart_object.data))
property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(force=False, bbox=None, layer_filter=None)

Deprecated, use composite().

Compose layer and masks (mask, vector mask, and clipping layers).

Note that the resulting image size is not necessarily equal to the layer size due to different mask dimensions. The offset of the composed image is stored at .info[‘offset’] attribute of PIL.Image.

Parameters:

bbox – Viewport bounding box specified by (x1, y1, x2, y2) tuple.

Returns:

PIL.Image, or None if the layer has no pixel.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

property effects

Layer effects.

Returns:

Effects

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property smart_object

Associated smart object.

Returns:

SmartObject.

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property width

Width of the layer.

Returns:

int

TypeLayer

class psd_tools.api.layers.TypeLayer(*args)[source]

Layer that has text and styling information for fonts or paragraphs.

Text is accessible at text property. Styling information for paragraphs is in engine_dict. Document styling information such as font list is is resource_dict.

Currently, textual information is read-only.

Example:

if layer.kind == 'type':
    print(layer.text)
    print(layer.engine_dict['StyleRun'])

    # Extract font for each substring in the text.
    text = layer.engine_dict['Editor']['Text'].value
    fontset = layer.resource_dict['FontSet']
    runlength = layer.engine_dict['StyleRun']['RunLengthArray']
    rundata = layer.engine_dict['StyleRun']['RunArray']
    index = 0
    for length, style in zip(runlength, rundata):
        substring = text[index:index + length]
        stylesheet = style['StyleSheet']['StyleSheetData']
        font = fontset[stylesheet['Font']]
        print('%r gets %s' % (substring, font))
        index += length
property bbox

(left, top, right, bottom) tuple.

property blend_mode

Blend mode of this layer. Writable.

Example:

from psd_tools.constants import BlendMode
if layer.blend_mode == BlendMode.NORMAL:
    layer.blend_mode = BlendMode.SCREEN
Returns:

BlendMode.

property bottom

Bottom coordinate.

Returns:

int

property clip_layers

Clip layers associated with this layer.

To compose clipping layers:

from psd_tools import compose
clip_mask = compose(layer.clip_layers)
Returns:

list of layers

property clipping_layer

Clipping flag for this layer. Writable.

Returns:

bool

compose(force=False, bbox=None, layer_filter=None)

Deprecated, use composite().

Compose layer and masks (mask, vector mask, and clipping layers).

Note that the resulting image size is not necessarily equal to the layer size due to different mask dimensions. The offset of the composed image is stored at .info[‘offset’] attribute of PIL.Image.

Parameters:

bbox – Viewport bounding box specified by (x1, y1, x2, y2) tuple.

Returns:

PIL.Image, or None if the layer has no pixel.

composite(viewport=None, force=False, color=1.0, alpha=0.0, layer_filter=None, apply_icc=False)

Composite layer and masks (mask, vector mask, and clipping layers).

Parameters:
  • viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the layer’s bbox.

  • force – Boolean flag to force vector drawing.

  • color – Backdrop color specified by scalar or tuple of scalar. The color value should be in [0.0, 1.0]. For example, (1., 0., 0.) specifies red in RGB color mode.

  • alpha – Backdrop alpha in [0.0, 1.0].

  • layer_filter – Callable that takes a layer as argument and returns whether if the layer is composited. Default is is_visible().

Returns:

PIL.Image.

property document_resources

Resource set relevant to the document.

property effects

Layer effects.

Returns:

Effects

property engine_dict

Styling information dict.

has_clip_layers()

Returns True if the layer has associated clipping.

Returns:

bool

has_effects()

Returns True if the layer has effects.

Returns:

bool

has_mask()

Returns True if the layer has a mask.

Returns:

bool

has_origination()

Returns True if the layer has live shape properties.

Returns:

bool

has_pixels()

Returns True if the layer has associated pixels. When this is True, topil method returns PIL.Image.

Returns:

bool

has_stroke()

Returns True if the shape has a stroke.

has_vector_mask()

Returns True if the layer has a vector mask.

Returns:

bool

property height

Height of the layer.

Returns:

int

is_group()

Return True if the layer is a group.

Returns:

bool

is_visible()

Layer visibility. Takes group visibility in account.

Returns:

bool

property kind

Kind of this layer, such as group, pixel, shape, type, smartobject, or psdimage. Class name without layer suffix.

Returns:

str

property layer_id

Layer ID.

Returns:

int layer id. if the layer is not assigned an id, -1.

property left

Left coordinate. Writable.

Returns:

int

property mask

Returns mask associated with this layer.

Returns:

Mask or None

property name

Layer name. Writable.

Returns:

str

numpy(channel=None, real_mask=True)

Get NumPy array of the layer.

Parameters:

channel – Which channel to return, can be ‘color’, ‘shape’, ‘alpha’, or ‘mask’. Default is ‘color+alpha’.

Returns:

numpy.ndarray or None if there is no pixel.

property offset

(left, top) tuple. Writable.

Returns:

tuple

property opacity

Opacity of this layer in [0, 255] range. Writable.

Returns:

int

property origination

Property for a list of live shapes or a line.

Some of the vector masks have associated live shape properties, that are Photoshop feature to handle primitive shapes such as a rectangle, an ellipse, or a line. Vector masks without live shape properties are plain path objects.

See psd_tools.api.shape.

Returns:

List of Invalidated, Rectangle, RoundedRectangle, Ellipse, or Line.

property parent

Parent of this layer.

property resource_dict

Resource set.

property right

Right coordinate.

Returns:

int

property size

(width, height) tuple.

Returns:

tuple

property stroke

Property for strokes.

property tagged_blocks

Layer tagged blocks that is a dict-like container of settings.

See psd_tools.constants.Tag for available keys.

Returns:

TaggedBlocks or None.

Example:

from psd_tools.constants import Tag
metadata = layer.tagged_blocks.get_data(Tag.METADATA_SETTING)
property text

Text in the layer. Read-only.

Note

New-line character in Photoshop is ‘\r’.

property top

Top coordinate. Writable.

Returns:

int

topil(channel=None, apply_icc=False)

Get PIL Image of the layer.

Parameters:
  • channel – Which channel to return; e.g., 0 for ‘R’ channel in RGB image. See ChannelID. When None, the method returns all the channels supported by PIL modes.

  • apply_icc – Whether to apply ICC profile conversion to sRGB.

Returns:

PIL.Image, or None if the layer has no pixels.

Example:

from psd_tools.constants import ChannelID

image = layer.topil()
red = layer.topil(ChannelID.CHANNEL_0)
alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)

Note

Not all of the PSD image modes are supported in PIL.Image. For example, ‘CMYK’ mode cannot include alpha channel in PIL. In this case, topil drops alpha channel.

property transform

Matrix (xx, xy, yx, yy, tx, ty) applies affine transformation.

property vector_mask

Returns vector mask associated with this layer.

Returns:

VectorMask or None

property visible

Layer visibility. Doesn’t take group visibility in account. Writable.

Returns:

bool

property warp

Warp configuration.

property width

Width of the layer.

Returns:

int