pytoshop.user.nested_layers

Convert a PSD file to/from nested layers.

class pytoshop.user.nested_layers.Group(name='', visible=True, opacity=255, group_id=0, blend_mode=<BlendMode.pass_through: b'pass'>, layers=None, closed=True, metadata=None, layer_color=0)[source]

Bases: pytoshop.user.nested_layers.Layer

A Layer that may contain other Layer instances.

closed

Is layer closed in GUI?

layers

List of sublayers

class pytoshop.user.nested_layers.Image(name='', visible=True, opacity=255, group_id=0, blend_mode=<BlendMode.normal: b'norm'>, top=0, left=0, bottom=None, right=None, channels=None, metadata=None, layer_color=0, color_mode=None)[source]

Bases: pytoshop.user.nested_layers.Layer

A Layer containing image data, i.e. a leaf node.

bottom

The bottom of the layer, in pixels. If not provided, will be automatically determined from channel data.

channels

The channel image data. May be one of the following

  • A dictionary from enums.ChannelId to 2-D numpy arrays.
  • A 3-D numpy array of the shape (num_channels, height, width)
  • A list of numpy arrays where each is a channel.

It is better to use get_channel and set_channel to

color_mode

The color mode of the image.

get_channel(color)[source]

Get a channel for a given color. Raises an error if the color space doesn’t have the given color.

Parameters:color (enums.ColorChannel) –
Returns:channel
Return type:2-D numpy array
left

The left of the layer, in pixels.

right

The right of the layer, in pixels. If not provided, will be automatically determined from channel data.

set_channel(color, channel)[source]

Get a channel for a given color. Raises an error if the color space doesn’t have the given color.

Parameters:
top

The top of the layer, in pixels.

class pytoshop.user.nested_layers.Layer[source]

Bases: object

Base class of all layers.

blend_mode

blend mode

group_id

Linked layer id

layer_color

layer color (as it appears in the layer list)

metadata
name

The name of the layer

opacity

Opacity. 0=transparent, 255=opaque

visible

Is layer visible?

pytoshop.user.nested_layers.nested_layers_to_psd(layers, color_mode, version=<Version.version_1: 1>, compression=<Compression.rle: 1>, depth=None, size=None, vector_mask=False)[source]

Convert a hierarchy of nested Layer instances to a PsdFile.

Parameters:
  • layers (list of Layer instances) – The hierarchy of layers we want to create.
  • color_mode (enums.ColorMode) – The color mode of the resulting PSD file (as well as the input image data).
  • version (enums.Version, optional) – The version of the PSD spec to follow.
  • compression (enums.Compression, optional) – The method of image compression to use for the layer image data.
  • depth (enums.ColorDepth, optional) – The color depth of the resulting image. Must match the color depth of the data passed in. If not provided, the color depth will be automatically determined from the passed-in data.
  • size (2-tuple of int, optional) – The shape in the form (height, width) of the resulting PSD file. If not provided, the height and width will be set to include all passed in layers, and the layers themselves will be adjusted so that none fall outside of the image.
  • vector_mask (bool, optional) – When True, the mask for the layer will be a vector rectangle. This results in much smaller file sizes, but is not quite as accurately rendered by Photoshop. When False, a raster mask is used.
Returns:

psdfile – The resulting PSD file.

Return type:

PsdFile

pytoshop.user.nested_layers.pprint_layers(layers, indent=0)[source]

Pretty-print a hierarchy of Layer instances.

pytoshop.user.nested_layers.psd_to_nested_layers(psdfile)[source]

Convert a PsdFile instance to a hierarchy of nested Layer instances.

Parameters:psdfile (PsdFile) – A parsed PSD file.
Returns:layers – A representation of the parsed hierarchy from the file.
Return type:list of Layer instances