写这个小工具可以把快速选择工具、魔棒、套索等直接填充颜色
1.gif (433.32 KB, 下载次数: 0)
下载附件
2025-1-30 04:11 上传
2.gif (281.9 KB, 下载次数: 0)
下载附件
2025-1-30 04:11 上传
image.png (17.49 KB, 下载次数: 0)
下载附件
2025-1-30 04:12 上传
代码:
[Python] 纯文本查看 复制代码import win32com.client
import pythoncom
import PySimpleGUI as sg
from time import sleep
from threading import Thread
CONNECT_STAT = "未连接"
SWITCH_ON = False
STAY_TOP = False
FILL_MODE = 0
EXPAND = 0.0
SEL_BOUNDS = ""
CLOSE_THREAD = False
RE_CONNECT = False
def connect_ps():
global CONNECT_STAT
ps = None
try:
pythoncom.CoInitialize()
ps = win32com.client.Dispatch("Photoshop.Application")
except pythoncom.com_error as e:
CONNECT_STAT = e.args[1]
else:
CONNECT_STAT = "已连接:" + ps.Application.Name + " " + ps.Version
finally:
return ps
def auto_fill():
global SEL_BOUNDS, RE_CONNECT
ps = connect_ps()
while not CLOSE_THREAD:
sleep(0.5)
if RE_CONNECT:
ps = connect_ps()
RE_CONNECT = False
# 主处理
if ps and SWITCH_ON:
try:
# 获取填充对象
match FILL_MODE:
case 0:
fill_mode = ps.ForegroundColor
case 1:
fill_mode = ps.BackgroundColor
case _:
fill_mode = ps.ForegroundColor
except pythoncom.com_error as e:
SEL_BOUNDS = e.args[1]
continue
try:
sel = ps.ActiveDocument.Selection
except pythoncom.com_error:
SEL_BOUNDS = "错误:可能当前没有打开文件"
continue
try:
# 检测选区是否存在
bounds = sel.Bounds
except pythoncom.com_error:
SEL_BOUNDS = "未检测到选区"
continue
else:
SEL_BOUNDS = str(bounds)
if EXPAND:
try:
# 扩展填充范围
expand_val = EXPAND
sel.Expand(expand_val)
except pythoncom.com_error as e:
SEL_BOUNDS = e.args[1]
continue
try:
# 填充
sel.Fill(fill_mode)
sel.Deselect()
except pythoncom.com_error as e:
SEL_BOUNDS = e.args[1]
continue
pythoncom.CoUninitialize()
def create_window():
sg.theme("DarkPurple")
sg.SetOptions(font=("Microsoft YaHei", 10))
layout = [
[sg.Button("重连"), sg.Text(CONNECT_STAT, key="CONNECT_STAT")],
[sg.HorizontalSeparator()],
[sg.Button("启用"), sg.Button("禁用", disabled=True), sg.Text(SEL_BOUNDS, key="SEL_BOUNDS")],
[sg.Radio("前景色", key="前景色", group_id=1, default=True),
sg.Radio("背景色", key="背景色", group_id=1)],
[sg.Text("越界填充:"),
sg.Spin(list(range(51)), key="EXPAND", size=5), sg.Text("像素")],
[sg.HorizontalSeparator()],
[sg.Button("置顶", key="STAY_TOP"), sg.Push(), sg.Button("关闭")]
]
window = sg.Window("PS选区填充", layout)
return window
def main():
global SWITCH_ON, FILL_MODE, STAY_TOP, CLOSE_THREAD, RE_CONNECT, EXPAND
fill_thread = Thread(target=auto_fill, name="FillThread")
fill_thread.start()
window = create_window()
while True:
event, values = window.read(timeout=100)
print(event, values)
# 窗口事件处理
match event:
case "重连":
RE_CONNECT = True
case "启用":
SWITCH_ON = True
window["启用"].update(disabled=True)
window["禁用"].update(disabled=False)
case "禁用":
SWITCH_ON = False
window["启用"].update(disabled=False)
window["禁用"].update(disabled=True)
case "STAY_TOP":
if STAY_TOP:
window["STAY_TOP"].update("置顶")
window.keep_on_top_clear()
STAY_TOP = False
else:
window["STAY_TOP"].update("取消置顶")
window.keep_on_top_set()
STAY_TOP = True
case None | "关闭":
break
# 窗口数值处理
if values["前景色"]:
FILL_MODE = 0
if values["背景色"]:
FILL_MODE = 1
EXPAND = float(values["EXPAND"])
# 更新文本
window["CONNECT_STAT"].update(CONNECT_STAT)
window["SEL_BOUNDS"].update(SEL_BOUNDS)
CLOSE_THREAD = True
fill_thread.join()
window.close()
if __name__ == "__main__":
main()
打包:
https://pan.baidu.com/s/1LbDJgnxuv4v29QAns038dQ?pwd=5q5n