Python PSD layers?

前端 未结 5 1369
野的像风
野的像风 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条回答
  •  借酒劲吻你
    2021-02-03 15:37

    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)
    
    

提交回复
热议问题