Python通過(guò)psd-tools解析PSD文件
前言:
最近碰到業(yè)務(wù)需要根據(jù)PSD文件實(shí)現(xiàn)PSD文件解析圖層功能,搜到了Python的一個(gè)解析PSD的庫(kù)。這個(gè)庫(kù)就是psd-tools
,psd-tools
是一個(gè)Python軟件包,用于處理Adobe Photoshop PSD文件。以下就是psd-tools
的基本介紹。
特點(diǎn)
支持:
- 讀取和寫入初級(jí)的PSD/PSB文件結(jié)構(gòu)
- 以NumPy和PIL格式導(dǎo)出原始圖層圖像
有限的支持:
- 基于像素的基本圖層的構(gòu)造
- 填充層效果的構(gòu)造
- 矢量面具
- 編輯一些圖層屬性,如圖層名稱
- 除溶解外的混合模式
- 繪制貝塞爾曲線
不支持:
- 編輯圖層結(jié)構(gòu),如添加或刪除一個(gè)圖層
- 調(diào)整層的構(gòu)造
- 許多層效果的構(gòu)造
- 字體渲染
安裝
使用pip來(lái)安裝該軟件包。
pip install psd-tools
為了用完整的圖層圖像合成功能,也可以安裝NumPy/SciPy:
pip install numpy scipy
使用
簡(jiǎn)單的例子:
from psd_tools import PSDImage psd = PSDImage.open('example.psd') psd.composite().save('example.png') for layer in psd: print(layer) layer_image = layer.composite() layer_image.save('%s.png' % layer.name)
1. 命令行
該軟件包提供命令行工具來(lái)處理PSD文件。
psd-tools export <input_file> <output_file> [options] psd-tools show <input_file> [options] psd-tools debug <input_file> [options] psd-tools -h | --help psd-tools --version
例子:
psd-tools show example.psd # 顯示文件內(nèi)容 psd-tools export example.psd example.png # 導(dǎo)出為PNG psd-tools export example.psd[0] example-0.png # 將圖層導(dǎo)出為PNG
2. 操作PSD文件
psd_tools.api
包提供了用戶友好的API來(lái)處理PSD文件。
打開(kāi)一個(gè)圖像:
from psd_tools import PSDImage psd = PSDImage.open('my_image.psd')
psd-tools
中的大部分?jǐn)?shù)據(jù)結(jié)構(gòu)都支持在IPython環(huán)境下的打?。?/p>
In [1]: PSDImage.open('example.psd') Out[1]: PSDImage(mode=RGB size=101x55 depth=8 channels=3) [0] PixelLayer('Background' size=101x55) [1] PixelLayer('Layer 1' size=85x46)
內(nèi)部層可以通過(guò)迭代器或索引進(jìn)行訪問(wèn):
for layer in psd: print(layer) if layer.is_group(): for child in layer: print(child) child = psd[0][0]
打開(kāi)的PSD文件可以保存:
psd.save('output.psd')
3. 操作使用層
在Photoshop中,有各種層的種類。
最基本的圖層類型是PixelLayer
:
print(layer.name) layer.kind == 'pixel'
有些圖層屬性是可編輯的,如圖層名稱:
layer.name = 'Updated layer 1'
組里有內(nèi)部層:
for layer in group: print(layer) first_layer = group[0]
TypeLayer
是一個(gè)帶有文本的層:
print(layer.text)
ShapeLayer
繪制一個(gè)矢量形狀,形狀信息存儲(chǔ)在vector_mask和origination屬性中。其他層也可以有形狀信息作為遮罩:
print(layer.vector_mask) for shape in layer.origination: print(shape)
SmartObjectLayer
嵌入或鏈接一個(gè)外部文件,用于非破壞性編輯。文件內(nèi)容可以通過(guò)smart_object屬性訪問(wèn):
import io if layer.smart_object.filetype in ('jpg', 'png'): image = Image.open(io.BytesIO(layer.smart_object.data))
SolidColorFill
, PatternFill
, 和 GradientFill
是填充圖層,如果沒(méi)有相關(guān)的遮罩,它們會(huì)繪制整個(gè)區(qū)域。 AdjustmentLayer
的子類表示應(yīng)用于組成圖像的層調(diào)整。參見(jiàn) Adjustment layers.
4. 將數(shù)據(jù)導(dǎo)出到 PIL
將整個(gè)文件導(dǎo)出為 PIL.Image
:
image = psd.composite() image.save('exported.png')
導(dǎo)出單一圖層,包括遮罩和剪裁層:
image = layer.composite()
分別導(dǎo)出圖層和蒙版,不需要合成:
image = layer.topil() mask = layer.mask.topil()
要合成特定的圖層,如除文本外的圖層,請(qǐng)使用layer_filter選項(xiàng):
image = psd.composite( layer_filter=lambda layer: layer.is_visible() and layer.kind != 'type')
請(qǐng)注意:大多數(shù)圖層效果和調(diào)整層不被支持。合成的結(jié)果可能看起來(lái)與Photoshop不同。
4. 將數(shù)據(jù)導(dǎo)出到NumPy
PSDImage或圖層可以通過(guò) numpy()
方法導(dǎo)出為NumPy數(shù)組:
image = psd.numpy() layer_image = layer.numpy()
更多操作
1. 操作一個(gè)PSD文件
可在源碼的psd_image.py中看到PSDImage類
1. 打開(kāi)一個(gè)文件
from psd_tools import PSDImage psd = PSDImage.open('my_image.psd') #返回一個(gè)PSDImage類型的對(duì)象 #psd_tools中的大多數(shù)數(shù)據(jù)結(jié)構(gòu)都支持在IPython環(huán)境中進(jìn)行漂亮的打印。 # ?In [1]: PSDImage.open('example.psd') # ?Out[1]: # ?PSDImage(mode=RGB size=101x55 depth=8 channels=3) # ? ?[0] PixelLayer('Background' size=101x55) # ? ?[1] PixelLayer('Layer 1' size=85x46)
2. psd的屬性(可在源碼的psd_image.py中看到PSDImage類)
有些無(wú)意義的屬性也定義了,為了和layer一樣可以,如:visible直接返回Ture。
這里列出一些有意義,一般會(huì)用到的屬性:
psd.width #寬 psd.height #高 psd.size #(width, height) tuple psd.offset #(left, top) tuple psd.left #0 psd.right #self.width psd.top #0 psd.bottom #self.height psd.viewbox #(left, top, right, bottom) `tuple` psd.bbox #能包圍住所有可見(jiàn)的層的最小的方框(x,y,z,w) psd.color_mode #顏色模式,如RGB,GRAYSCALE psd.channels #顏色通道數(shù)量 psd.depth #像素深度位數(shù) psd.version #文件版本 psd是1,psb是2. psd.has_preview #Returns if the document has real merged data. When True, `topil()`returns pre-composed data. psd.has_thumbnail #是否有縮略圖 psd.thumbnail #返回?? ?PIL.Image格式的縮略圖
這里列出一些無(wú)意義的為了可以和layer一樣操作的屬性:
psd.is_visible() #True psd.visible #True psd.parent #None psd.name ? #'Root' psd.kind #'psdimage' print(str(psd.is_group()))#是否是組 psd文件直接傳進(jìn)去也是組 psd文件的層可以遍歷: for layer in psd: print(layer) if layer.is_group(): ? ? for child in layer: ? ? ? ? print(child) child = psd[0][0] #迭代順序是從背景到前景,與1.7.x之前的版本相反。使用reverse (list(psd))從前臺(tái)到后臺(tái)進(jìn)行迭代。
3. 保存psd文件
psd.save('output.psd')
4. 用psd文件獲取PIL Image.
psd.topil(channel=None, **kwargs) #channel:0為R,1為G,2為B,-1為A,根據(jù)constants.py中ChannelID類。
5. 合并psd文件.
psd.compose(force = False,bbox=None,**kwargs)
6. 用PIL Image生成一個(gè)PSDImage對(duì)象
from psd_tools import PSDImage psd = PSDImage.frompilfrompil(image,compression=<Compression.PACK_BITS: 1>)
2. 操作一個(gè)PSD圖層
可在源碼的layers.py中看到Layer類
1.Layer的屬性(可在源碼的layers.py中看到Layer類)
layer.name #層的名字(可寫) layer.kind #層的類別(字符串) #(group(圖層組), pixel(普通圖層), shape, type(文本圖層), smartobject,or psdimage(psd本身)) #shape繪制矢量形狀,形狀信息存儲(chǔ)在vector_mask和origination屬性中。其他圖層也可以有形狀信息作為蒙版: #smartobject為非破壞性編輯嵌入或鏈接外部文件。文件內(nèi)容可以通過(guò)smart_object屬性訪問(wèn)。 layer.layer_id #Layer ID. layer.visible #層本身是否勾選可見(jiàn)(可寫) layer.is_visible() #層是否可見(jiàn),受父物體影響。(父物體不可見(jiàn),這個(gè)層就算勾選了可見(jiàn)這個(gè)也是False) layer.opacity #透明度 [0,255](可寫) layer.parent #Parent of this layer. layer.is_group #是否是個(gè)組 layer.blend_mode #混合模式(可寫),返回Constants.py中的BlendMode layer.has_mask #是否有mask layer.left #左坐標(biāo)(可寫) layer.top ?#頂坐標(biāo)(可寫) layer.right #右坐標(biāo) layer.bottom #底坐標(biāo) layer.width #層的寬 layer.height #層的高 layer.offset #(left, top) tuple. (可寫) layer.size #(width, height) tuple. layer.bbox #(left, top, right, bottom) tuple. layer.has_pixels() #是否有像素 layer.has_mask() #是否有蒙板 layer.has_vector_mask() #是否有矢量蒙板 layer.mask #層相關(guān)的蒙版 return: :py:class:`~psd_tools.api.mask.Mask` or `None` layer.vector_mask #層相關(guān)的矢量蒙版 return: :py:class:`~psd_tools.api.shape.VectorMask` or `None` layer.has_origination() #是否有實(shí)時(shí)形狀屬性 layer.origination #實(shí)時(shí)形狀屬性 layer.has_stroke() #是否有比劃 layer.stroke #比劃 layer.has_clip_layers() #是否有裁剪 layer.clip_layers #裁剪,Clip layers associated with this layer. layer.has_effects() #是否有效果處理 layer.effects #效果處理 return: :py:class:`~psd_tools.api.effects.Effects` layer.tagged_blocks #Layer tagged blocks that is a dict-like container of settings.
2. 獲得圖層的pil圖,Get PIL Image of the layer.(返回PIL.Image對(duì)象或沒(méi)像素時(shí)返回`None`)
layer.topil(channel=None, **kwargs) e.g. from psd_tools.constants import ChannelID image = layer.topil() red = layer.topil(ChannelID.CHANNEL_0) alpha = layer.topil(ChannelID.TRANSPARENCY_MASK)
3. 合并圖層和其蒙版(mask, vector mask, and clipping layers)(返回PIL.Image對(duì)象或沒(méi)像素時(shí)返回`None`)
layer.compose(bbox=None, **kwargs)
不合并,單獨(dú)獲?。?/strong>
image = layer.topil() mask = layer.mask.topil() from psd_tools import compose clip_image = compose(layer.clip_layers)
到此這篇關(guān)于Python通過(guò)psd-tools解析PSD文件的文章就介紹到這了,更多相關(guān)Python PSD 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch 利用lstm做mnist手寫數(shù)字識(shí)別分類的實(shí)例
今天小編就為大家分享一篇pytorch 利用lstm做mnist手寫數(shù)字識(shí)別分類的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01使用PySpark實(shí)現(xiàn)數(shù)據(jù)清洗與JSON格式轉(zhuǎn)換的實(shí)踐詳解
在大數(shù)據(jù)處理中,PySpark?提供了強(qiáng)大的工具來(lái)處理海量數(shù)據(jù),特別是在數(shù)據(jù)清洗和轉(zhuǎn)換方面,本文將介紹如何使用?PySpark?進(jìn)行數(shù)據(jù)清洗,并將數(shù)據(jù)格式轉(zhuǎn)換為?JSON?格式的實(shí)踐,感興趣的可以了解下2023-12-12淺談pycharm導(dǎo)入pandas包遇到的問(wèn)題及解決
這篇文章主要介紹了淺談pycharm導(dǎo)入pandas包遇到的問(wèn)題及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python?創(chuàng)建或讀取?Excel?文件的操作代碼
Excel是一種常用的電子表格軟件,廣泛應(yīng)用于金融、商業(yè)和教育等領(lǐng)域,本文介紹Python?創(chuàng)建或讀取?Excel?文件的操作代碼,感興趣的朋友一起看看吧2023-09-09搭建python django虛擬環(huán)境完整步驟詳解
這篇文章主要介紹了搭建python django虛擬環(huán)境完整步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python通過(guò)OpenPyXL處理Excel的完整教程
OpenPyXL是一個(gè)強(qiáng)大的Python庫(kù),用于處理Excel文件,允許讀取、編輯和創(chuàng)建Excel工作簿和工作表,本文將詳細(xì)介紹OpenPyXL的各種功能,希望對(duì)大家有所幫助2023-11-11python 指定源路徑來(lái)解決import問(wèn)題的操作
這篇文章主要介紹了python 指定源路徑來(lái)解決import問(wèn)題的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Django中針對(duì)基于類的視圖添加csrf_exempt實(shí)例代碼
這篇文章主要介紹了Django中針對(duì)基于類的視圖添加csrf_exempt實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Python?Pygame實(shí)戰(zhàn)之歡樂(lè)打地鼠游戲
打地鼠是一款比較經(jīng)典的敏捷類游戲,我們可以在許多商場(chǎng)門口看到這類游戲機(jī),在電腦和手機(jī)上也有許多類似的游戲。本文將用Python中的Pygame庫(kù)實(shí)現(xiàn)這一游戲,需要的可以參考一下2022-02-02python實(shí)現(xiàn)批量視頻分幀、保存視頻幀
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量視頻分幀、保存視頻幀,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05