Python PSD图层?

6

我需要编写一个Python程序,用于加载PSD的Photoshop图像,该图像具有多个图层,并将其转换为PNG文件(每个图层一个文件)。

您能否在Python中完成此操作?我已经尝试使用PIL,但似乎没有访问图层的方法。求助!

另外,编写自己的PSD加载程序和PNG编写程序速度太慢了。

6个回答

5

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

这样就不需要Photoshop了,而且可以在运行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 针对 psdparse!看起来 OP 不必自己编写功能 :) - rzetterberg
1
我们已经尝试了所有可能的选择,我相信你只有两种选择:要么自己动手开发,要么使用Gimp-Python。 - agf
太糟糕了。我可能能够编写自己的代码,但它可能会变得太慢(Python)。有什么建议可以尝试吗?我想PIL使用基于插件的方法,所以我可以尝试一下。有人回复了一个带有示例插件的答案,但它已经被删除了。 - Brock123
也许可以尝试联系psdparse的作者,或者至少以他的模块为基础。没有必要完全从头开始。如果我的回答有帮助,请接受它。 - agf
2
这是个好主意。那么,如何“接受”答案呢?我想投票,但我还不能。 - Brock123

3

在Python中使用psd_tools

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

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

1

1
您可以使用win32com来使用Python访问Photoshop。 下面是您工作的伪代码:
1. 加载PSD文件 2. 收集所有图层并使所有图层的VISIBLE属性为OFF 3. 逐个打开每个图层,将其VISIBLE属性标记为ON并导出为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文件名及路径' doc.SaveAs(SaveIn=tf,Options=options) ly.Visible = False #pApp.Open(PSD文件) doc = pApp.ActiveDocument makeAllLayerInvisible(doc.Layers) makeEachLayerVisibleAndExportToPNG(doc.Layers)

win32com是用于Windows的。 - Maksym Ganenko
win32com是用于Windows的。 - undefined

0

https://github.com/antipalindrome/Photoshop-Export-Layers-to-Files-Fast

这不是用Python写的,但是这里提供的解决方案对我来说比上面的脚本更具挑战性。如果你想要做更多的事情而不仅仅是将图层导出为单独的文件,那么你可能最好使用这里的其他选项。但是如果你只关心将图层导出为文件,这个脚本非常简单和快速地实现了这个功能。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接