I need to write a Python program for loading a PSD photoshop image, which has multiple layers and spit out png files (one for each layer). Can you do that in Python? I've tried PIL, but there doesn't seem to be any method for accessing layers. Help. PS. Writing my own PSD loader and png writer has shown to be way too slow.
6 Answers
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.

- 171,228
- 44
- 289
- 238
-
+1 For `psdparse`! Seems like OP doesn't have to roll his/her own functionality :) – rzetterberg Jul 20 '11 at 11:16
-
1We've exhaused all of the options out there, I believe. You'll either have to roll your own, or use Gimp-Python. – agf Jul 20 '11 at 16:17
-
Thats too bad. I might be able to write my own code, but it might end up too slow (Python). Any suggestions what to try? i guess PIL uses plugin based approach so I could try that. There was a reply with example plugin, but it got removed – Brock123 Jul 20 '11 at 17:00
-
Maybe try and get in touch with the psdparse author, or at least use his module as a basis. No reason to start completely from scratch. Please accept my answer if I've helped. – agf Jul 20 '11 at 17:03
-
2That's a good idea. Er, how to "accept" answer? Id vote, but I can't yet – Brock123 Jul 20 '11 at 17:37
Use psd_tools in Python
from psd_tools import PSDImage
psd_name = "your_name"
x = 0
psd = PSDImage.open('your_file.psd')
for layer in psd:
x+=1
if layer.kind == "smartobject":
image.conmpose().save(psd_name + str(x) + "png")

- 67
- 9
Using the win32com plugin for python (available here: http://python.net/crew/mhammond/win32/) You can access photoshop and easily go through your layers and export them.
Here is a code sample that works on the layers within the currently active Photoshop document, and exports them into the folder defined in 'save_location'.
from win32com.client.dynamic import Dispatch
#Save location
save_location = 'c:\\temp\\'
#call photoshop
psApp = Dispatch('Photoshop.Application')
options = Dispatch('Photoshop.ExportOptionsSaveForWeb')
options.Format = 13 # PNG
options.PNG8 = False # Sets it to PNG-24 bit
doc = psApp.activeDocument
#Hide the layers so that they don't get in the way when exporting
for layer in doc.layers:
layer.Visible = False
#Now go through one at a time and export each layer
for layer in doc.layers:
#build the filename
savefile = save_location + layer.name + '.png'
print 'Exporting', savefile
#Set the current layer to be visible
layer.visible = True
#Export the layer
doc.Export(ExportIn=savefile, ExportAs=2, Options=options)
#Set the layer to be invisible to make way for the next one
layer.visible = False

- 56
- 7
There are also https://code.google.com/p/pypsd/ and https://github.com/kmike/psd-tools Python packages for reading PSD files.

- 21,908
- 8
- 73
- 65
You can use the win32com for accessing the Photoshop with Python. Possible pseudo code for your work:
- Load the PSD file
- Collect all layers and make all layers VISIBLE=OFF
- 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)

- 19
- 1
https://github.com/antipalindrome/Photoshop-Export-Layers-to-Files-Fast
This is not in python, but the suggested solutions here proved more challenging for me than the above script. If you want the capacity to do more than just export layers to individual files, you're probably better off using the other options here. But if you only care about exporting layers to files, this script works easily and very quickly.

- 11
- 3