我需要编写一个用于加载PSD photoshop图像的Python程序,该图像具有多个图层并吐出png文件(每个图层一个)。 你可以用Python做到吗?我试过PIL,但似乎没有任何访问图层的方法。帮帮我。 PS。写我自己的PSD加载器和PNG作家已经显示出太慢了。Python PSD图层?
6
A
回答
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)]
编辑:OK,找到了解决办法:https://github.com/jerem/psdparse
这将允许你提取从蟒蛇PSD文件层没有任何非Python的东西。
2
您可以使用win32com通过Python访问Photoshop。为您的工作 可能的伪代码:
- 装入PSD文件
- 收集所有层,使所有层的可见= OFF
- 打开了一个又一个层,标志着它们可见= 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 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)
1
使用了蟒蛇的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
还有https://code.google.com/p/pypsd/和https://github.com/kmike/psd-tools用于读取PSD文件的Python包。
相关问题
- 1. Python阅读多层PSD?
- 2. 如何使用PSD-plugin为Paint.NET保存png中的psd图层?
- 3. 从psd中提取分组图层
- 4. 如何从PSD文件获取图层?
- 5. PIL - 空PSD图像
- 6. 从PSD文件构建层合成
- 7. PHP将psd转换为jpg,选择图像层
- 8. 如何在Xcode中显示或隐藏psd文件的图层
- 9. 使用JQuery从PSD图层创建动画帧
- 10. 读一个Photoshop(PSD)的RLE压缩图层文件
- 11. PHP:获取PSD文件图层的位置(x,y)
- 12. 如何从包含单层图像的PSD文件中提取图像
- 13. 如何使用psd-python工具
- 14. 从PSD
- 15. 是否有工具可以比较复杂PSD文件中的图层?
- 16. 用于读取和写入多层.psd图像的开放源码
- 17. 如何将jpg或png文件转换为带有多个图层的psd?
- 18. 如何在PSD文件中获取图层关系或组信息?
- 19. android psd库使用PSD格式
- 20. 计算PSD
- 21. 使用Python&PyKML修改地图图层
- 22. 是否可以将png转换为分层PSD文件?
- 23. 如何检测使用imageIO.framework的多层psd文件?
- 24. PSD分层来引导3个最佳实践
- 25. 在C#中创建简单的分层PSD文件
- 26. 从网页(PHP或JS)创建分层PSD或TIFF
- 27. 如何从命令行创建分层PSD文件?
- 28. PSD规范帮助
- 29. 同一图中的多个PSD - Matlab
- 30. PSD与图片切片背景
+1对于'psdparse'!似乎OP不必滚动他/她自己的功能:) – rzetterberg
psdparse不起作用。 “不支持的频道数量”错误... – Brock123
我相信,我们已经用尽了所有的选项。你要么必须自己推出,要么使用Gimp-Python。 – agf