comon.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import pyautogui
  4. from scriptBase.imgFind import pcacc_img
  5. from scriptBase.mouseClick import pcacc_mouse
  6. import time
  7. import random
  8. import pygetwindow as gw
  9. import re
  10. from PIL import Image
  11. from PIL import ImageFilter
  12. import io
  13. from scriptBase.ocr import *
  14. from scriptBase.record import ScreenRecorder
  15. g_recorder = ScreenRecorder()
  16. def compress_image(image, target_size = 100 * 1024):
  17. # 调整图像尺寸,保持纵横比
  18. image.thumbnail((1024, 1024))
  19. # 尝试不同的压缩质量
  20. quality = 80
  21. compressed_image = io.BytesIO()
  22. while compressed_image.tell() < target_size and quality >= 10:
  23. image.save(compressed_image, format='JPEG', quality=quality)
  24. quality -= 10
  25. compressed_image.seek(0)
  26. compressed_size = len(compressed_image.getvalue())
  27. return compressed_image.getvalue()
  28. '''
  29. def binarize_image(image):
  30. # 将图片转换为黑白格式(二值化)
  31. # 这里使用Pillow库来进行图片二值化
  32. image = image.convert("L") # 将图片转换为灰度图像
  33. threshold = 128 # 设定二值化阈值,0~255之间,通常取128
  34. image = image.point(lambda x: 0 if x < threshold else 255, '1') # 进行二值化处理
  35. return image
  36. '''
  37. def binarize_image(image):
  38. # 将图片转换为灰度图像
  39. image = image.convert("L")
  40. # 降噪处理(中值滤波)
  41. image = image.filter(ImageFilter.MedianFilter(size=3))
  42. # 自适应阈值二值化
  43. threshold = image.point(lambda x: 0 if x < 128 else 255, '1')
  44. return threshold
  45. def waitFindImg(imgPath, waitTime, preWaitTime, isCV=False):
  46. if preWaitTime > 0:
  47. myTimeSleep(preWaitTime)
  48. #sleep 0.2s per times
  49. curTime = 0
  50. while waitTime == -1 or curTime < waitTime:
  51. ret, pos_cur = pcacc_img.find_imgs(imgPath, isCV)
  52. if ret:
  53. return ret, pos_cur
  54. else:
  55. myTimeSleep(0.2)
  56. curTime = curTime + 0.2
  57. print("waitFindImg failed", imgPath)
  58. return False, None
  59. def waitFindImg_withBool(imgPath, waitTime, preWaitTime, isCV=False):
  60. ret, pos = waitFindImg(imgPath, waitTime, preWaitTime, isCV)
  61. return ret
  62. def waitClickImg_noWait(imgPath, waitTime, preWaitTime, isCV=False):
  63. if preWaitTime > 0:
  64. myTimeSleep(preWaitTime)
  65. #sleep 0.2s per times
  66. curTime = 0
  67. while waitTime == -1 or curTime < waitTime:
  68. ret, pos_cur = pcacc_img.find_imgs(imgPath, isCV)
  69. if ret:
  70. pcacc_mouse.click(pos_cur[0], pos_cur[1])
  71. myTimeSleep(0.5)
  72. return True, pos_cur
  73. else:
  74. myTimeSleep(0.2)
  75. curTime = curTime + 0.2
  76. print("waitClickImg_noWait failed", imgPath)
  77. return False, (-1, -1)
  78. def waitClickImg_noWait_withBool(imgPath, waitTime, preWaitTime, isCV=False):
  79. ret, pos = waitClickImg_noWait(imgPath, waitTime, preWaitTime, isCV)
  80. return ret
  81. def waitclickMultiImg(imgArr, waitTime, preWaitTime):
  82. if preWaitTime > 0:
  83. myTimeSleep(preWaitTime)
  84. #sleep 0.2s per times
  85. curTime = 0
  86. while waitTime == -1 or curTime < waitTime:
  87. ret, pos_cur = pcacc_img.find_imgArr(imgArr)
  88. if ret:
  89. pcacc_mouse.click(pos_cur[0], pos_cur[1])
  90. myTimeSleep(0.5)
  91. ret, pos_cur = pcacc_img.find_imgArr(imgArr)
  92. if ret == False:
  93. return True, pos_cur
  94. pcacc_mouse.click(pos_cur[0], pos_cur[1])
  95. myTimeSleep(0.5)
  96. ret, pos_cur = pcacc_img.find_imgArr(imgArr)
  97. if ret == False:
  98. return True, pos_cur
  99. else:
  100. return True, pos_cur
  101. else:
  102. myTimeSleep(0.2)
  103. curTime = curTime + 0.2
  104. print("waitclickMultiImg failed", imgArr)
  105. return False, (-1, -1)
  106. def waitClickImg(imgPath, waitTime, preWaitTime, isCV=False):
  107. if preWaitTime > 0:
  108. myTimeSleep(preWaitTime)
  109. #sleep 0.2s per times
  110. curTime = 0
  111. while waitTime == -1 or curTime < waitTime:
  112. ret, pos_cur = pcacc_img.find_imgs(imgPath, isCV)
  113. if ret:
  114. pcacc_mouse.move_mouse(pos_cur[0], pos_cur[1])
  115. ret, pos_cur2 = pcacc_img.find_imgs(imgPath, isCV)
  116. if ret:
  117. pcacc_mouse.click(pos_cur2[0], pos_cur2[1])
  118. else:
  119. pcacc_mouse.click(pos_cur[0], pos_cur[1])
  120. myTimeSleep(0.5)
  121. ret, pos_cur = pcacc_img.find_imgs(imgPath, isCV)
  122. if ret == False:
  123. return True, pos_cur
  124. pcacc_mouse.move_mouse(pos_cur[0], pos_cur[1])
  125. ret, pos_cur2 = pcacc_img.find_imgs(imgPath, isCV)
  126. if ret:
  127. pcacc_mouse.click(pos_cur2[0], pos_cur2[1])
  128. else:
  129. pcacc_mouse.click(pos_cur[0], pos_cur[1])
  130. myTimeSleep(0.5)
  131. ret, pos_cur = pcacc_img.find_imgs(imgPath, isCV)
  132. if ret == False:
  133. return True, pos_cur
  134. else:
  135. return True, pos_cur
  136. else:
  137. myTimeSleep(0.2)
  138. curTime = curTime + 0.2
  139. print("waitClickImg failed", imgPath)
  140. return False, (-1, -1)
  141. def waitClickImg_withBool(imgPath, waitTime, preWaitTime, isCV=False):
  142. ret, pos = waitClickImg(imgPath, waitTime, preWaitTime, isCV)
  143. return ret
  144. def myTimeSleep(curTime , FunStatus=None):
  145. tmpTime = curTime * 1000
  146. randomTime = random.randint(int(tmpTime - tmpTime / 9), int(tmpTime + tmpTime / 9)) / 1000
  147. randomTime = round(randomTime)
  148. while randomTime > 50:
  149. time.sleep(50)
  150. randomTime -= 50
  151. showStr = f'下一轮{randomTime}s'
  152. if FunStatus is not None:
  153. FunStatus(showStr)
  154. print(showStr)
  155. time.sleep(randomTime)
  156. def myTimeSleep_small():
  157. time.sleep(random.randint(200, 1000) / 1000)
  158. def myTimeSleep_big():
  159. time.sleep(random.randint(1200, 2000) / 1000)
  160. def get_random_point(region):
  161. left, top, width, height = region
  162. x = random.randint(left, left + width)
  163. y = random.randint(top, top + height)
  164. return x, y
  165. def get_yys_random_point(region):
  166. dstRegion = yys_getRegion(region, True)
  167. print(dstRegion)
  168. return get_random_point(dstRegion)
  169. def maintain_state(min):
  170. state = True
  171. last_reset_time = time.time()
  172. def inner_func():
  173. nonlocal state, last_reset_time
  174. # 获取当前时间
  175. current_time = time.time()
  176. # 如果距离上次重置的时间大于等于min秒,则将状态重置为True
  177. if current_time - last_reset_time >= min:
  178. state = True
  179. last_reset_time = current_time
  180. # 返回状态值,并将状态置为False
  181. result = state
  182. state = False
  183. return result
  184. return inner_func
  185. def find_window_pos(title):
  186. # 查找窗口
  187. winList = gw.getWindowsWithTitle(title)
  188. if len(winList) == 0:
  189. return False, None
  190. window = gw.getWindowsWithTitle(title)[0] # 替换为你要查找的窗口标题
  191. # 获取位置信息
  192. left = window.left
  193. top = window.top
  194. width = window.width
  195. height = window.height
  196. # 打印位置信息
  197. #print(f'Left: {left}, Top: {top}, Width: {width}, Height: {height}')
  198. if left > 0 and top > 0 and width > 0 and height > 0:
  199. return True, (left, top, width, height)
  200. else:
  201. return False, None
  202. def find_yys_pos():
  203. return find_window_pos('Mumu模拟器12')
  204. def get_first_single_digit(string):
  205. pattern = r'\d' # 匹配一个独立的单个数字
  206. match = re.search(pattern, string)
  207. if match:
  208. return int(match.group()) # 将匹配到的字符串转换为整数
  209. else:
  210. return None
  211. def dragAuto(gamestr, positions):
  212. tmpRet, tmpPos = find_window_pos(gamestr)
  213. if tmpRet == False:
  214. return False
  215. x1 = positions[0] + tmpPos[0] + random.randint(-20, 20)
  216. y1 = positions[1] + tmpPos[1] + random.randint(-20, 20)
  217. x2 = positions[2] + tmpPos[0] + random.randint(-20, 20)
  218. y2 = positions[3] + tmpPos[1] + random.randint(-20, 20)
  219. pcacc_mouse.drag_from(x1, y1, x2, y2)
  220. return True
  221. def ocrAuto(gamestr, positions):
  222. try:
  223. tmpRet, tmpPos = find_window_pos(gamestr)
  224. if tmpRet == False:
  225. return []
  226. x1 = positions[0] + tmpPos[0]
  227. y1 = positions[1] + tmpPos[1]
  228. x2 = positions[2] + tmpPos[0]
  229. y2 = positions[3] + tmpPos[1]
  230. region = (x1, y1, x2, y2)
  231. lineStr = capture_and_ocr(region)
  232. print("识别到:", lineStr)
  233. except:
  234. lineStr = ""
  235. return lineStr
  236. def yys_ocrAuto(positions):
  237. return ocrAuto('Mumu模拟器12', positions)
  238. def yys_dragAuto(positions):
  239. return dragAuto('Mumu模拟器12', positions)
  240. def getRegion(gamestr, region, isWidth):
  241. tmpRet, tmpPos = find_window_pos(gamestr)
  242. if tmpRet == False:
  243. return False
  244. if isWidth:
  245. return (region[0] + tmpPos[0], region[1] + tmpPos[1], region[2] - region[0], region[3] - region[1])
  246. else:
  247. return (region[0] + tmpPos[0], region[1] + tmpPos[1], region[2] + tmpPos[0], region[3] + tmpPos[1])
  248. def game_region():
  249. tmpRet, tmpPos = find_window_pos('Mumu模拟器12',)
  250. return tmpRet, tmpPos
  251. def save_game_screen():
  252. regionRet, regionPos = game_region()
  253. screenshot = pyautogui.screenshot(region=regionPos)
  254. curData = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
  255. fileName = "temp_pictures/" + curData + ".png"
  256. screenshot.save(fileName)
  257. return True
  258. def start_recording():
  259. global g_recorder
  260. regionRet, regionPos = game_region()
  261. print("record region,", regionPos)
  262. g_recorder.start_recording(regionPos)
  263. def stop_recording():
  264. global g_recorder
  265. g_recorder.stop_recording()
  266. def yys_getRegion(region, isWidth= False):
  267. return getRegion('Mumu模拟器12', region, isWidth)
  268. def click_waitimg(pos_x,pos_y,img):
  269. retry_times = 3
  270. while retry_times > 0 and waitFindImg_withBool(img,0.4,1) == False:
  271. pcacc_mouse.click(pos_x,pos_y)
  272. retry_times-=1
  273. return retry_times > 0