6

我需要编写一个 Python 程序来加载 PSD photoshop 图像,该图像具有多个图层并吐出 png 文件(每层一个)。你能在 Python 中做到这一点吗?我试过 PIL,但似乎没有任何方法可以访问图层。帮助。PS。编写我自己的 PSD 加载器和 png 编写器已经证明太慢了。

4

5 回答 5

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 内容。

于 2011-07-20T09:23:28.823 回答
2

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

  1. 加载 PSD 文件
  2. 收集所有图层并使所有图层 VISIBLE=OFF
  3. 一层一层地翻,把它们标记为 VISIBLE=ON 并导出为 PNG
    导入 win32com.client
    pApp = win32com.client.Dispatch('Photoshop.Application')

    def makeAllLayerInvisible(lyrs):
        lyrs 中的 ly:
            ly.Visible = 假

    def makeEachLayerVisibleAndExportToPNG(lyrs):
        lyrs 中的 ly:
            ly.Visible = 真
            options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
            options.Interlaced = False
            tf = '带路径的PNG文件名'
            doc.SaveAs(SaveIn=tf,Options=options)
            ly.Visible = 假

    #pApp.Open(PSD 文件)
    doc = pApp.ActiveDocument
    makeAllLayerInvisible(doc.Layers)
    makeEachLayerVisibleAndExportToPNG(doc.Layers)

于 2011-09-12T18:42:25.993 回答
2

在 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")
于 2019-06-13T07:37:46.357 回答
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
于 2012-04-04T02:36:27.840 回答
1

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

于 2012-11-03T18:56:28.203 回答