使用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)

有问题可以随时反馈

相关推荐
AI算法沐枫28 分钟前
机器学习到底是什么?
人工智能·python·深度学习·机器学习·数据挖掘·大模型·#ai
小技与小术30 分钟前
玩转Flask
开发语言·python·flask
SilentSamsara37 分钟前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速
开发语言·python·青少年编程·性能优化
冰小忆42 分钟前
大驼峰命名规范和小驼峰命名规范的区别是什么?
开发语言·python
高洁011 小时前
知识图谱:AI的超级大脑
人工智能·python·数据挖掘·知识图谱
知识分享小能手1 小时前
Flask入门学习教程,从入门到精通,Flask智能租房——前期准备 知识点详解(5)
python·学习·flask
Curvatureflight1 小时前
【架构实战】生产级大模型 API 接入指南:流式响应(Streaming)异常处理与监控闭环
python·架构
smj2302_796826522 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
deepin_sir2 小时前
06 Chroma_持久化与生产环境部署实战
python
wuxinyan1233 小时前
工业级大模型学习之路021:LangChain零基础入门教程(第四篇):文档加载与文本分块技术
人工智能·python·学习·langchain