【发布时间】:2011-07-20 09:06:58
【问题描述】:
我需要编写一个 Python 程序来加载 PSD photoshop 图像,该图像具有多个图层并输出 png 文件(每个图层一个)。 你能在 Python 中做到这一点吗?我试过 PIL,但似乎没有任何方法可以访问图层。帮助。 PS。编写我自己的 PSD 加载器和 png 编写器已被证明太慢了。
【问题讨论】:
标签: python image python-imaging-library layer psd
我需要编写一个 Python 程序来加载 PSD photoshop 图像,该图像具有多个图层并输出 png 文件(每个图层一个)。 你能在 Python 中做到这一点吗?我试过 PIL,但似乎没有任何方法可以访问图层。帮助。 PS。编写我自己的 PSD 加载器和 png 编写器已被证明太慢了。
【问题讨论】:
标签: python image python-imaging-library layer psd
使用 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 内容。
【讨论】:
psdparse!似乎OP不必推出他/她自己的功能:)
您可以使用 win32com 通过 Python 访问 Photoshop。 您的工作可能的伪代码:
【讨论】:
在 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")
【讨论】:
使用适用于 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
【讨论】:
还有https://code.google.com/p/pypsd/和https://github.com/kmike/psd-tools Python包用于读取PSD文件。
【讨论】: