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

有问题可以随时反馈

相关推荐
好家伙VCC17 小时前
**TensorFlow:发散创新的深度学习框架探索**随着人工智
java·人工智能·python·深度学习·tensorflow
YFLICKERH17 小时前
【多进线程】python多进线程与通信
python
程序员爱钓鱼17 小时前
Python编程实战 · 基础入门篇 | 第一个Python程序:Hello World
后端·python·编程语言
川石课堂软件测试18 小时前
CSS中常用的几种定位。
开发语言·css·python·网络协议·http·html·pytest
C.R.xing18 小时前
Pyspark分布式访问NebulaGraph图数据库
数据库·分布式·python·pyspark·nebulagraph
我是华为OD~HR~栗栗呀18 小时前
华为OD-21届考研-Java面经
java·前端·c++·python·华为od·华为·面试
松果集19 小时前
【2】数据结构·序列构成的数组
python
局外人LZ19 小时前
django rest framework:从零开始搭建RESTful API
python·django·restful·drf
㏕追忆似水年华あ19 小时前
逻辑600解析本03
python·flask