Skip to main content

操作psd文件

https://www.figma.com/file/iL6CnbMbOohoYZYyv1SNPZ/Untitled?node-id=0%3A1

修改字体相关

# -*- coding: utf-8 -*-
#
# @Author: CPS
# @email: 373704015@qq.com
# @Date:
# @Last Modified by: CPS
# @Last Modified time: 2022-04-28 15:09:28.168782
# @file_path "W:\CPS\MyProject\test"
# @Filename "test.py"
# @Description: 功能描述
#

# Import local modules
from photoshop import Session
import photoshop





# style 2

def test(target):
# style 1
app = photoshop.api.Application()
app.load(target)

for i in app.documents:
print('修改前图层名称:', i.artLayers[0].textItem)
# print("修改前文字内容:", i.artLayers[0].textItem.contents)
print("修改前文字字体:", i.artLayers[0].textItem.font)

# print("修改前文字颜色:", i.artLayers[0].textItem.color.rgb)
# print("修改前文字大小:", i.artLayers[0].textItem.size)
# print("修改前文字位置:", i.artLayers[0].textItem.position)

# print('修改后文字为: 020')
# i.artLayers[0].textItem.contents = '020'
# print("修改后文字大小:", i.artLayers[0].textItem.size)
# print("修改后文字位置:", i.artLayers[0].textItem.position)
i.artLayers[0].textItem.font = 'system'
i.artLayers[0].textItem.size = 300

# i.artLayers[0].textItem.font = '仿宋'
print("修改前文字字体:", i.artLayers[0].textItem.font)

# with Session(target, action="open") as ps:
# ps.echo(ps.active_document.name)
# docRef = ps.app.activeDocument

# textlayer = ps.active_document.artLayers.getByName('textlayer')
# print('psd文件信息:')
# print('psd文件尺寸:', f'({docRef.width}, {docRef.height})')

# print('\n文字图层[textlayer]信息:')
# print("文字内容:", textlayer.textItem.contents)
# print("文字颜色:", textlayer.textItem.color.rgb)
# print("文字大小:", textlayer.textItem.size)
# print("文字位置:", textlayer.textItem.position)
# print("文字对象:", textlayer.textItem.font)


# # print("文字对象:", textlayer.textItem.text_font)
# # print("文字对象:", textlayer.textItem.parent.font)
# print('文件对象: ', dir(textlayer.textItem))

# new_contents = 'capsion'
# # textlayer.textItem.contents = new_contents
# # print("修改文字内容为: ", new_contents)

if ( __name__ == "__main__"):
target = r"C:\Users\Administrator\Downloads\tee_test_new.psd"

test(target)

使用win32 api

https://www.bilibili.com/read/cv6457043/

import win32com.client

# Pieced together from
# http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html
# and
# http://rubypane.blogspot.com/2013/01/sample-python-script-to-control.html


psApp = win32com.client.Dispatch("Photoshop.Application")
psApp.Open(r"C:\temp\blah.psd")         # Opens a PSD file
doc = psApp.Application.ActiveDocument  # Get active document object

layer = doc.ArtLayers[2]                # Get the bottom-most layer
layers = doc.artLayers

artLayerRef = layers.add
artLayerRef.kind = 2 #Text layer
# Set the contents of the text layer.

textItemRef = artLayerRef.TextItem
textItemRef.Contents = "Hello, web!"

doc.Save()

未整理

# -*- coding: utf-8 -*-
#
# @Author: CPS
# @email: 373704015@qq.com
# @Date:
# @Last Modified by: CPS
# @Last Modified time: 2022-04-30 15:44:23.645151
# @file_path "D:\CPS\MyProject\test"
# @Filename "s.py"
# @Description: 功能描述
#

from typing import *
from psd_tools import PSDImage
from os import path

from psd_tools.psd import PSD

class TLayerInfo(TypedDict):
name:str
size:list[int]
layer_type:str
layer_id:int
has_mask:bool

def get_font_layer_info(layer):
if layer.kind != 'type': return
print(dir(layer))
print("layer.text: ", layer.text)
print('layer.kind: ', layer.kind)

def get_smart_object_info(layer):
if layer.smart_object.is_psd():
with layer.smart_object.open() as f:
print('\n')
print(f'打开智能对象【 {layer.smart_object.filename} 】: ')
smart_object = PSDImage.open(f)
for each in smart_object:
get_layer_info(each)
print('\n')

# print(dir(layer.smart_object))

def get_group_info(group):
for layers in group:
get_layer_info(layers)


def get_layer_info(layer, only_visible:bool=False) -> 'TLayerInfo':
if only_visible and layer.visible == False: return
print(f'Open Layer: {layer.name}')
res = {
'visible':layer.is_visible(),
'name':layer.name,
"size":layer.size,
'layer_type':layer.kind,
'has_mask':layer.has_mask(),
"layer_id":layer.layer_id
}

# print(res)
match str(layer.kind):
case 'group':
get_group_info(layer)
case 'smartobject':
get_smart_object_info(layer)
case 'type':
print(f'【!发现文字图层!】:')
get_font_layer_info(layer)

return res

if ( __name__ == "__main__"):
target = path.abspath(r'./test_file/3.psd')
psd = PSDImage.open(target)
p = PSD.open(target)

print(f'根图层数量({len(psd)})')
print(f'{p.linked_layer}')
for layer in psd:
get_layer_info(layer)

print('\n')
# print(dir(psd[0]))
# psd.composite().save('example.png')