最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python通過(guò)psd-tools解析PSD文件

 更新時(shí)間:2022年06月16日 16:24:41   作者:??xpsilvester????  
這篇文章主要介紹了Python通過(guò)psd-tools解析PSD文件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下

前言:

最近碰到業(yè)務(wù)需要根據(jù)PSD文件實(shí)現(xiàn)PSD文件解析圖層功能,搜到了Python的一個(gè)解析PSD的庫(kù)。這個(gè)庫(kù)就是psd-toolspsd-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文件。

打開一個(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]

打開的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))

SolidColorFillPatternFill, 和 GradientFill 是填充圖層,如果沒(méi)有相關(guān)的遮罩,它們會(huì)繪制整個(gè)區(qū)域。 AdjustmentLayer 的子類表示應(yīng)用于組成圖像的層調(diào)整。參見 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. 打開一個(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 #能包圍住所有可見的層的最小的方框(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 #層本身是否勾選可見(可寫)
layer.is_visible() #層是否可見,受父物體影響。(父物體不可見,這個(gè)層就算勾選了可見這個(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ú)獲取:

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)文章

  • Python大批量寫入數(shù)據(jù)(百萬(wàn)級(jí)別)的方法

    Python大批量寫入數(shù)據(jù)(百萬(wàn)級(jí)別)的方法

    這篇文章主要給大家介紹了關(guān)于Python大批量寫入數(shù)據(jù)(百萬(wàn)級(jí)別)的相關(guān)資料,在日常處理數(shù)據(jù)的過(guò)程中,我們都有批量寫入數(shù)據(jù)的需求,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下
    2023-07-07
  • 編寫多線程Python服務(wù)器 最適合基礎(chǔ)

    編寫多線程Python服務(wù)器 最適合基礎(chǔ)

    很好的Python多線程基礎(chǔ)教程,能夠幫助初學(xué)者快速了解Python多線程簡(jiǎn)單實(shí)現(xiàn)。線程可以使任何程序運(yùn)行得更快。但是這也增加了代碼的復(fù)雜性。所以,如果你發(fā)現(xiàn)很難理解,那么添加更多的日志將有助于檢查里面發(fā)生了什么
    2018-09-09
  • python讀取excel指定列數(shù)據(jù)并寫入到新的excel方法

    python讀取excel指定列數(shù)據(jù)并寫入到新的excel方法

    今天小編就為大家分享一篇python讀取excel指定列數(shù)據(jù)并寫入到新的excel方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 深入理解?Python?中的?pip?虛擬環(huán)境(最佳實(shí)踐)

    深入理解?Python?中的?pip?虛擬環(huán)境(最佳實(shí)踐)

    本文深入講解了Python中pip虛擬環(huán)境的概念及其重要性,并詳細(xì)介紹了如何創(chuàng)建、激活和管理虛擬環(huán)境,以及如何使用requirements.txt文件記錄和管理項(xiàng)目依賴,文章指出,使用虛擬環(huán)境可以有效避免依賴沖突,為每個(gè)項(xiàng)目提供一個(gè)干凈的開發(fā)環(huán)境,使得項(xiàng)目更易于維護(hù)和部署
    2024-10-10
  • opencv對(duì)多種顏色小球的形狀及位置判斷方式

    opencv對(duì)多種顏色小球的形狀及位置判斷方式

    在這段時(shí)間參加了一個(gè)競(jìng)賽,寫下了這個(gè)代碼,但是總感覺(jué)有一些地方是不完善!這是一個(gè)關(guān)于使用opencv庫(kù)判斷顏色小球形狀及位置的功能實(shí)現(xiàn),其中也參考了一些前輩的代碼,希望能對(duì)迷茫中的小伙幫有所幫助
    2022-11-11
  • Python網(wǎng)絡(luò)爬蟲四大選擇器用法原理總結(jié)

    Python網(wǎng)絡(luò)爬蟲四大選擇器用法原理總結(jié)

    這篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲四大選擇器用法原理總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 解決keras+flask模型的重復(fù)調(diào)用出錯(cuò)ValueError: Tensor is not an element of this graph

    解決keras+flask模型的重復(fù)調(diào)用出錯(cuò)ValueError: Tensor is n

    這篇文章主要介紹了解決keras+flask模型的重復(fù)調(diào)用出錯(cuò)ValueError: Tensor is not an element of this graph問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換

    Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換

    這篇文章主要介紹了Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • 詳解Python之?dāng)?shù)據(jù)序列化(json、pickle、shelve)

    詳解Python之?dāng)?shù)據(jù)序列化(json、pickle、shelve)

    本篇文章主要介紹了Python之?dāng)?shù)據(jù)序列化,本節(jié)要介紹的就是Python內(nèi)置的幾個(gè)用于進(jìn)行數(shù)據(jù)序列化的模塊,有興趣的可以了解一下。
    2017-03-03
  • python腳本當(dāng)作Linux中的服務(wù)啟動(dòng)實(shí)現(xiàn)方法

    python腳本當(dāng)作Linux中的服務(wù)啟動(dòng)實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python腳本當(dāng)作Linux中的服務(wù)啟動(dòng)實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06

最新評(píng)論

宜黄县| 皮山县| 南溪县| 旌德县| 静乐县| 白城市| 余干县| 利川市| 涟源市| 盘锦市| 军事| 翁源县| 迭部县| 文成县| 如皋市| 湟源县| 博爱县| 东明县| 平塘县| 十堰市| 廊坊市| 阿坝县| 北票市| 新泰市| 正安县| 株洲县| 延边| 屯昌县| 芜湖市| 东乡县| 昭平县| 镇康县| 波密县| 普兰店市| 荔波县| 北宁市| 连云港市| 宁波市| 虎林市| 克山县| 富源县|