我需要編寫一個用於加載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