The package provides command line tools to handle a PSD document:
psd-tools export <input_file> <output_file> [options] psd-tools show <input_file> [options] psd-tools debug <input_file> [options] psd-tools -h | --help psd-tools --version
psd-tools show example.psd # Show the file content psd-tools export example.psd example.png # Export as PNG psd-tools export example.psd example-0.png # Export layer as PNG
Working with PSD document¶
psd_tools.api package provides the user-friendly API to work
with PSD files.
PSDImage represents a PSD file.
Open an image:
from psd_tools import PSDImage psd = PSDImage.open('my_image.psd')
Most of the data structure in the
psd-tools suppports pretty
printing in IPython environment.
In : PSDImage.open('example.psd') Out: PSDImage(mode=RGB size=101x55 depth=8 channels=3)  PixelLayer('Background' size=101x55)  PixelLayer('Layer 1' size=85x46)
Internal layers are accessible by iterator or indexing:
for layer in psd: print(layer) if layer.is_group(): for child in layer: print(child) child = psd
The iteration order is from background to foreground, which is
reversed from version prior to 1.7.x. Use
iterate from foreground to background.
The opened PSD file can be saved:
Working with Layers¶
There are various layer kinds in Photoshop.
The most basic layer type is
print(layer.name) layer.kind == 'pixel'
Some of the layer attributes are editable, such as a layer name:
layer.name = 'Updated layer 1'
Currently, the package does not support adding or removing of a layer.
Group has internal layers:
for layer in group: print(layer) first_layer = group
TypeLayer is a layer with texts:
ShapeLayer draws a vector shape, and the
shape information is stored in vector_mask and origination property.
Other layers can also have shape information as a mask:
print(layer.vector_mask) for shape in layer.origination: print(shape)
SmartObjectLayer embeds or links an
external file for non-destructive editing. The file content is accessible
via smart_object property:
import io if layer.smart_object.filetype in ('jpg', 'png'): image = Image.open(io.BytesIO(layer.smart_object.data))
GradientFill are fill layers that
paint the entire region if there is no associated mask. Sub-classes of
AdjustmentLayer represents layer
adjustment applied to the composed image. See Adjustment layers.
Exporting data to PIL¶
Export the entire document as
image = psd.composite() image.save('exported.png')
Export a single layer including masks and clipping layers:
image = layer.composite()
Export layer and mask separately without composition:
image = layer.topil() mask = layer.mask.topil()
To composite specific layers, such as layers except for texts, use layer_filter option:
image = psd.composite( layer_filter=lambda layer: layer.is_visible() and layer.kind != 'type')
Note that most of the layer effects and adjustment layers are not supported. The compositing result may look different from Photoshop.