comon.py 9.4 KB

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