comon.py 8.3 KB

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