我需要编写一个Python程序,用于加载PSD的Photoshop图像,该图像具有多个图层,并将其转换为PNG文件(每个图层一个文件)。
您能否在Python中完成此操作?我已经尝试使用PIL,但似乎没有访问图层的方法。求助!
另外,编写自己的PSD加载程序和PNG编写程序速度太慢了。
我需要编写一个Python程序,用于加载PSD的Photoshop图像,该图像具有多个图层,并将其转换为PNG文件(每个图层一个文件)。
您能否在Python中完成此操作?我已经尝试使用PIL,但似乎没有访问图层的方法。求助!
另外,编写自己的PSD加载程序和PNG编写程序速度太慢了。
使用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的工具。
在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")
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 用于读取PSD文件的Python包。
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)
https://github.com/antipalindrome/Photoshop-Export-Layers-to-Files-Fast
这不是用Python写的,但是这里提供的解决方案对我来说比上面的脚本更具挑战性。如果你想要做更多的事情而不仅仅是将图层导出为单独的文件,那么你可能最好使用这里的其他选项。但是如果你只关心将图层导出为文件,这个脚本非常简单和快速地实现了这个功能。
psdparse
!看起来 OP 不必自己编写功能 :) - rzetterberg