PSD levels for Python?

I need to write a Python program to load a Photoshop PSD image that has several layers and spits out png files (one for each layer). Can you do it in Python? I tried PIL, but there seems to be no way to access the layers. Help. PS. Writing your own PSD downloader and png record showed that it was too slow.

+6
source share
5 answers

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

You do not need Photoshop in this way, and it should work on any platform that runs Gimp and Python. This is a big addiction, but free.

To do this in PIL:

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

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

This will allow you to extract layers from the psd file using python without any things other than python.

+5
source

You can use win32com to access Photoshop using Python. Possible pseudo code for your work:

  • Download the PSD file
  • Collect all the layers and make all the layers VISIBLE = OFF
  • Rotate 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)

+2
source

Using 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 an example of code that runs on layers in the currently active Photoshop document and exports them to the folder specified 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 
+1
source

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

+1
source

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") 
0
source

Source: https://habr.com/ru/post/893141/


All Articles