Python PSD图层?

时间:2011-07-20 09:06:59

标签: python image python-imaging-library layer psd

我需要编写一个Python程序来加载PSD photoshop图像,它有多个图层并吐出png文件(每层一个)。 你能用Python做到吗?我尝试过PIL,但似乎没有任何方法可以访问图层。救命。 PS。编写我自己的PSD加载器和png编写器已经显示出太慢了。

5 个答案:

答案 0 :(得分:5)

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

你不需要那种方式,它应该适用于任何运行Gimp和Python的平台。这是一个很大的依赖,但是一个免费的。

在PIL中这样做:

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

编辑:好的,找到解决方案:https://github.com/jerem/psdparse

这将允许你使用python从psd文件中提取图层而不需要任何非python东西。

答案 1 :(得分:2)

您可以使用win32com通过Python访问Photoshop。 您工作的可能伪代码:

  1. 加载PSD文件
  2. 收集所有图层并使所有图层可见=关闭
  3. 逐层转动一层,将它们标记为VISIBLE = ON并导出为PNG
  4. 
        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 :(得分:1)

使用python的win32com插件(可在此处获取:http://python.net/crew/mhammond/win32/)您可以访问photoshop并轻松浏览图层并导出它们。

这是一个代码示例,它适用于当前活动的Photoshop文档中的图层,并将它们导出到“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

答案 3 :(得分:1)

还有用于读取PSD文件的https://code.google.com/p/pypsd/https://github.com/kmike/psd-tools Python包。

答案 4 :(得分:0)

在Python中使用psd_tools

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns3:sendSms xmlns:ns2="" xmlns:ns3=""><customerId>CUSTOM</customerId><customerType>PHYSICAL</customerType><phoneAlgorithm>last_updated_first</phoneAlgorithm><text>teXT</text><smsFlag>true</smsFlag><callingSystemName>blankpin</callingSystemName></ns3:sendSms></soap:Body></soap:Envelope>
相关问题