psd_tools¶
See Usage for examples.
PSDImage¶
- class psd_tools.PSDImage(data)[source]¶
Photoshop PSD/PSB file object.
The low-level data structure is accessible at
PSDImage._record
.Example:
from psd_tools import PSDImage psd = PSDImage.open('example.psd') image = psd.compose() for layer in psd: layer_image = layer.compose()
- property 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.
- property bottom¶
Bottom coordinate.
- Returns
int
- property channels¶
Number of color channels.
- Returns
int
- compose(force=False, bbox=None, layer_filter=None)[source]¶
Deprecated, use
composite()
.Compose the PSD image.
- 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, ignore_preview=False)[source]¶
Composite the PSD image.
- Parameters
viewport – Viewport bounding box specified by (x1, y1, x2, y2) tuple. Default is the viewbox of the PSD.
ignore_preview – Boolean flag to whether skip compositing when a pre-composited preview is available.
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 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.RLE)[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.
- property height¶
Document height.
- Returns
int
- property image_resources¶
Document image resources.
ImageResources
is a dict-like structure that keeps various document settings.See
psd_tools.constants.Resource
for available keys.- Returns
Example:
from psd_tools.constants import Resource version_info = psd.image_resources.get_data(Resource.VERSION_INFO) slices = psd.image_resources.get_data(Resource.SLICES)
Image resources contain an ICC profile. The following shows how to export a PNG file with embedded ICC profile:
from psd_tools.constants import Resource icc_profile = psd.image_resources.get_data(Resource.ICC_PROFILE) image = psd.compose(apply_icc=False) image.save('output.png', icc_profile=icc_profile)
- property kind¶
Kind.
- Returns
‘psdimage’
- property left¶
Left coordinate.
- Returns
0
- property 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.
- numpy(channel=None)[source]¶
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
- property offset¶
(left, top) tuple.
- Returns
tuple
- classmethod open(fp, **kwargs)[source]¶
Open a PSD document.
- Parameters
fp – filename or file-like object.
encoding – charset encoding of the pascal string within the file, default ‘macroman’. Some psd files need explicit encoding option.
- Returns
A
PSDImage
object.
- property parent¶
Parent of this layer.
- property right¶
Right coordinate.
- Returns
int
- save(fp, mode='wb', **kwargs)[source]¶
Save the PSD file.
- Parameters
fp – filename or file-like object.
encoding – charset encoding of the pascal string within the file, default ‘macroman’.
mode – file open mode, default ‘wb’.
- property size¶
(width, height) tuple.
- Returns
tuple
- property tagged_blocks¶
Document 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 patterns = psd.tagged_blocks.get_data(Tag.PATTERNS1)
- thumbnail()[source]¶
Returns a thumbnail image in PIL.Image. When the file does not contain an embedded thumbnail image, returns None.
- property top¶
Top coordinate.
- Returns
0
- topil(channel=None, apply_icc=False)[source]¶
Get PIL Image.
- 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 composed image is not available.
- property version¶
Document version. PSD file is 1, and PSB file is 2.
- Returns
int
- property viewbox¶
Return bounding box of the viewport.
- Returns
(left, top, right, bottom) tuple.
- property visible¶
Visibility.
- Returns
True
- property width¶
Document width.
- Returns
int
compose¶
- psd_tools.compose(layers, force=False, bbox=None, layer_filter=None, context=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 modes: dissolve and darker/lighter color becomes normal
Shape drawing is inaccurate if the PSD file is not saved with maximum compatibility.
Some of the blending modes do not reproduce photoshop blending.
- 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.
context – PIL.Image object for the backdrop rendering context. Must be used with the correct bbox size.
layer_filter – a callable that takes a layer and returns bool.
color – background color in int or tuple.
kwargs – arguments passed to underling topil() call.
- Returns
PIL.Image
or None.