使用python脚本大批量自动化处理图片上的ai水印

python 复制代码
import os
import time
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import traceback
from pywinauto import  mouse

def get_app_and_main_window(app_path):
    app = Application(backend="uia").start(app_path)
    # app = Application(backend="uia").connect(path=app_path)
    # 等待应用程序加载
    time.sleep(1)
    # 获取主窗口
    main_window = app.window(title_re=".*Inpaint.*", control_type='Window')
    # main_window.print_control_identifiers()
    main_window.wait('visible', timeout=10)
    print("已连接到应用程序主窗口")
    return app, main_window


def open_file_with_ctrl_o(app, file_path):
    """
    使用 Ctrl+O 快捷键打开文件对话框并选择文件

    参数:
        app_path: 应用程序路径
        file_path: 要打开的文件路径
    """
    try:

        # 激活窗口确保接收键盘输入
        main_window = app.top_window()
        main_window.set_focus()
        # # 使用 Ctrl+O 打开文件对话框
		# # 常用的控制快捷键说明 
        # SHIFT +,CTRL ^,ALT %,空格键 {SPACE},BACKSPACE {BACKSPACE}、
        # {BS} or {BKSP},BREAK {BREAK},CAPS LOCK {CAPSLOCK},
        # DEL or DELETE {DELETE} or {DEL},DOWN ARROW {DOWN},
        # END {END},ENTER {ENTER} or ~,ESC {ESC},HELP {HELP},
        # HOME {HOME},INS or INSERT {INSERT} or {INS},LEFT ARROW {LEFT},
        # NUM LOCK {NUMLOCK},PAGE DOWN {PGDN},
        # PAGE UP {PGUP},PRINT SCREEN {PRTSC},
        # RIGHT ARROW {RIGHT},SCROLL LOCK {SCROLLLOCK},
        # TAB {TAB},UP ARROW {UP},+ {ADD},- {SUBTRACT},
        # * {MULTIPLY},/ {DIVIDE},
        send_keys('^o')  # ^ 代表 Ctrl 键
        print("已发送 Ctrl+O 打开文件对话框")
        # # 打印所有控件信息
        # # main_window.print_control_identifiers()
        # # 打开文件对话框,并点击打开
        main_window.child_window(title="文件名(N):", class_name="Edit").set_text(file_path)
        main_window.child_window(title="打开(O)", class_name="Button").click()
        print("已确认文件选择并打开")
        time.sleep(1)
        # 移动鼠标,左上角水印位置
        left_top_watermark_position = (742, 125)
        # 移动鼠标,右下角水印位置
        right_down_top_watermark_position = (824, 125)

        mouse.move(coords=left_top_watermark_position)
        # 指定位置,鼠标左击
        mouse.click(button='left', coords=left_top_watermark_position)
        # 将属性移动到(140,40)坐标处按下
        mouse.press(button='left', coords=left_top_watermark_position)
        # 将鼠标移动到(300,40)坐标处释放,
        mouse.release(button='left', coords=right_down_top_watermark_position)
        # 移动鼠标,左上角水印位置
        left_top_watermark_position_2 = (1133, 970)
        # 移动鼠标,右下角水印位置
        right_down_top_watermark_position_2 = (1250, 1000)
        mouse.move(coords=left_top_watermark_position_2)
        # 指定位置,鼠标左击
        mouse.click(button='left', coords=left_top_watermark_position_2)
        # 将属性移动到(140,40)坐标处按下
        mouse.press(button='left', coords=left_top_watermark_position_2)
        # 将鼠标移动到(300,40)坐标处释放,
        mouse.release(button='left', coords=right_down_top_watermark_position_2)
        print("水印区域框选完成")

        top_window=app.top_window()
        # top_window.print_control_identifiers()

        # 点击处理图像
        chuli_window=top_window.child_window(title="处理图像", control_type="Button")
        chuli_window.click()
        time.sleep(10)
        #  保存
        send_keys('^s')  # ^ 代表 Ctrl 键
        print("已发送 Ctrl+s 保存文件对话框")

        # 另存为
        # top_window.child_window(title="另存为", control_type="Button").click() #这里执行click后,程序会阻塞,暂时忽略
        # top_window_2=app.top_window()
        # top_window_2.print_control_identifiers()
        #
        # # # 打开文件对话框,并点击打开
        # top_window_2.set_focus()
        # top_window_2.child_window(title="文件名(N):", class_name="Edit").set_text(out_file_path)
        # top_window_2.child_window(title="保存(S)", class_name="Button").click()
        return True

    except Exception as e:
        # print(f"操作过程中出现错误: {e}")
        traceback.print_exc()
        return False

def batch_replace_image(app,main_window,file_path):
    try:
        for i in os.listdir(file_path):
            if i.endswith('.png') or i.endswith('.jpg'):
                image_path = os.path.join(file_path, i)
                open_file_with_ctrl_o(app,image_path)
                # print(i)
    except Exception as e:
        # print(f"操作过程中出现错误: {e}")
        traceback.print_exc()
        return False

def app_kill(app):
    try:
        app.kill()
        print("已关闭应用程序")
    except Exception as e:
        print(f"关闭应用程序时出现错误: {e}")



# 使用示例
if __name__ == "__main__":
    inpaint_path = r'D:\exe安装包\Inpaint_v9.1_x64.exe'
    file_path = r'E:\AI生成\AI绘本\10月15日'
    app, main_window = get_app_and_main_window(inpaint_path)
    batch_replace_image(app,main_window,file_path)
    app_kill(app)

有问题可以随时反馈

相关推荐
段一凡-华北理工大学7 小时前
工业智能体:从Codex/Claude到项目实战~系列文章05:从“AI助手“到“工业智能体“:能力跃迁与落地范式
人工智能·python·claude·codex·工业智能体·高炉炼铁智能化
深蓝AI7 小时前
ContextVar 到线程池就丢了?asyncio.to_thread 和 run_in_executor 实测
python
岁月宁静9 小时前
什么是 Agent:从"会聊天"到"会做事"
人工智能·python
岁月宁静9 小时前
AI 应用开发工程师简历怎么写:HR 初筛、技术复筛、面试追问,三道关卡各看什么?
人工智能·python·面试
来让爷抱一个9 小时前
Python 异步里混用同步库,事件循环被卡住的全过程
开发语言·python
小肝一下11 小时前
3.基础语法(3)
python
深蓝AI12 小时前
Python 子进程树:父进程被杀后,孙进程为何还在跑?
python
心祭丫丫12 小时前
【每天一个 Python 小案例17】Python 实现通讯录
开发语言·人工智能·笔记·python·学习
爱你真是太好了12 小时前
Skill 索引瘦身与渐进加载
python
贾公子12 小时前
检测链路的容量规划——用 Little's Law 算清 QPS、并发与积压
后端·python