Python PSD layers?

前端 未结 5 1371
野的像风
野的像风 2021-02-03 14:39

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

5条回答
  •  梦毁少年i
    2021-02-03 15:22

    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
    

提交回复
热议问题