程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python operates psd to save a transparent layer of the same length and width as the original image

編輯:Python

文章目錄

  • python操作psdSave a transparent layer of the same length and width as the original image
    • 1. 思路
    • 2. 代碼實現

python操作psdSave a transparent layer of the same length and width as the original image

1. 思路

  1. 獲取psd對象

  2. Turn on the visibility of layers that need to be saved

  3. 保存psd

  4. 重新獲取psd對象(Tried saving layers while manipulating visibility,不可行)

  5. Generate a transparent image with the same length and height as the original image,Merge with layers,Save the merged layer

2. 代碼實現

from psd_tools import PSDImage
from PIL import Image
import os
# 圖片融合
def mix(img1, img2, coordinator):
""" :param img1: :param img2: :param coordinator: The layer is on the transparent mapleft,top坐標 :return: """
im = img1
mark = img2
layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
layer.paste(mark, coordinator)
out = Image.composite(layer, im, layer)
return out
def change_visible():
psd_path = r'G:\20220803\全景\000001\000001_000000.psd'
psd = PSDImage.open(psd_path) # 打開psd對象
for index, layer in enumerate(psd.descendants()):
layer_name = layer.name # 圖層名稱
if layer_name.count("背景"): # 過濾掉背景
layer.visible = False
continue
layer.visible = True
psd.save(psd_path) # 保存
def get_save_layer_png():
psd_path = r'G:\20220803\全景\000001\000001_000000.psd'
basename = os.path.basename(psd_path).replace(".psd", "")
save_png_dir_path = os.path.join(os.path.dirname(psd_path), basename)
if not os.path.exists(save_png_dir_path):
os.makedirs(save_png_dir_path)
psd = PSDImage.open(psd_path) # 打開psd對象
image = Image.new(mode='RGBA', size=psd.size) # Generates transparent images of equal height and width
for index, layer in enumerate(psd.descendants()):
print(layer.size) # 圖層大小
layer_name = layer.name # 圖層名稱
if layer_name.count("背景"): # 過濾掉背景
continue
layer.composite().save("tmp.png")
file1 = Image.open("tmp.png")
co = (layer.left, layer.top) # Determine the coordinate position of the upper left point of the layer's transparent image
mix_res = mix(image, file1, co)
mix_res.save(os.path.join(save_png_dir_path, layer.name + ".png"))
# print(mix_res)
os.remove("tmp.png")
if __name__ == '__main__':
change_visible()
get_save_layer_png()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved