Python PSD layers?

Use Gimp-Python? http://www.gimp.org/docs/python/index.html

You don't need Photoshop that way, and it should work on any platform that runs Gimp and Python. It's a large dependency, but a free one.

For doing it in PIL:

from PIL import Image, ImageSequence
im = Image.open("spam.psd")
layers = [frame.copy() for frame in ImageSequence.Iterator(im)]

Edit: OK, found the solution: https://github.com/jerem/psdparse

This will allow you to extract layers from a psd file with python without any non-python stuff.


You can use the win32com for accessing the Photoshop with Python. Possible pseudo code for your work:

  1. Load the PSD file
  2. Collect all layers and make all layers VISIBLE=OFF
  3. Turn one layer after another, mark them VISIBLE=ON and export to PNG
    import win32com.client
    pApp = win32com.client.Dispatch('Photoshop.Application')

    def makeAllLayerInvisible(lyrs):
        for ly in lyrs:
            ly.Visible = False

    def makeEachLayerVisibleAndExportToPNG(lyrs):
        for ly in lyrs:
            ly.Visible = True
            options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
            options.Interlaced = False
            tf = 'PNG file name with path'
            doc.SaveAs(SaveIn=tf,Options=options)
            ly.Visible = False

    #pApp.Open(PSD file)
    doc = pApp.ActiveDocument
    makeAllLayerInvisible(doc.Layers)
    makeEachLayerVisibleAndExportToPNG(doc.Layers)