1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import pyautogui import schedule import time import os from itertools import product from datetime import datetime import pyperclip import sys import keyboard
input_command1 = "/bl" input_command2 = "end"
box1_position = (958, 643) box2_position = (1142, 616) box3_position = (1369, 648) folder1_path = 'D:/midjourney相关/midjourney_PythonAuto/f1' folder2_path = 'D:/midjourney相关/midjourney_PythonAuto/f2' folder3_path = 'D:/midjourney相关/midjourney_PythonAuto/f3' file_delay = 1
index = 0 combinations = [] paused = False auto_paused = False last_active_time = time.time() pause_start_time = None auto_pause_start_time = None total_pause_time = 0
def get_current_time(): return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def get_files(folder_path): return [os.path.join(folder_path, file_name) for file_name in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, file_name))]
def open_file_dialog_and_select_files(file_path, box_position): print(f"{get_current_time()} - 打开文件对话框并选择文件:{file_path}") pyautogui.click(box_position) time.sleep(2)
pyautogui.hotkey('ctrl', 'l') time.sleep(1) pyperclip.copy(os.path.dirname(file_path)) time.sleep(1) pyautogui.hotkey('ctrl', 'v') pyautogui.press('enter') time.sleep(1) pyautogui.click(662, 643) time.sleep(1) pyperclip.copy(os.path.basename(file_path)) time.sleep(1) pyautogui.hotkey('ctrl', 'v') time.sleep(1) pyautogui.press('enter') time.sleep(file_delay)
def execute_blend_command(): print(f"{get_current_time()} - 执行/blend命令") time.sleep(2) pyautogui.click(965, 853) pyautogui.typewrite(input_command1) time.sleep(1) pyautogui.typewrite(input_command2) pyautogui.press('enter') time.sleep(1) pyautogui.click(1315, 849) time.sleep(1) pyautogui.click(896, 433) time.sleep(2)
def init_combinations(): global combinations files1 = get_files(folder1_path) files2 = get_files(folder2_path) files3 = get_files(folder3_path) combinations = list(product(files1, files2, files3))
def task(): global index, paused, auto_paused, last_active_time, total_pause_time, auto_pause_start_time
current_time = time.time() elapsed_time = current_time - last_active_time
if paused or auto_paused: if auto_paused and auto_pause_start_time is None: auto_pause_start_time = current_time return
if auto_pause_start_time is not None: total_pause_time += current_time - auto_pause_start_time auto_pause_start_time = None
active_time = elapsed_time - total_pause_time
if active_time >= 120: auto_paused = True print(f"{get_current_time()} - 自动暂停。\n\n") return
if index < len(combinations): file1, file2, file3 = combinations[index] print(f"{get_current_time()} - 处理组合:{os.path.basename(file1)} 和 {os.path.basename(file2)} 和 {os.path.basename(file3)}") execute_blend_command() open_file_dialog_and_select_files(file1, box1_position) open_file_dialog_and_select_files(file2, box2_position) open_file_dialog_and_select_files(file3, box3_position) time.sleep(1) pyautogui.press('enter') time.sleep(1) pyautogui.press('enter') time.sleep(1) index += 1 else: print(f"{get_current_time()} - 所有组合已处理完成。") schedule.cancel_job(job) sys.exit()
def toggle_pause(): global paused, pause_start_time, total_pause_time, auto_pause_start_time paused = not paused if paused: pause_start_time = time.time() auto_pause_start_time = None print(f"{get_current_time()} - 任务已暂停。\n\n") else: if pause_start_time is not None: total_pause_time += time.time() - pause_start_time pause_start_time = None print(f"{get_current_time()} - 任务已继续。\n\n")
def continue_auto_pause(): global auto_paused, last_active_time, total_pause_time, auto_pause_start_time auto_paused = False last_active_time = time.time() total_pause_time = 0 auto_pause_start_time = None print(f"{get_current_time()} - 自动暂停已解除。\n\n")
def listen_keyboard(): keyboard.add_hotkey('p', toggle_pause) keyboard.add_hotkey('o', continue_auto_pause)
init_combinations()
job = schedule.every(1.5).minutes.do(task)
listen_keyboard()
while True: schedule.run_pending() time.sleep(1)
|
评论