comon.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 , FunStatus=None):
  141. tmpTime = curTime * 1000
  142. randomTime = random.randint(int(tmpTime - tmpTime / 9), int(tmpTime + tmpTime / 9)) / 1000
  143. while randomTime > 10:
  144. time.sleep(10)
  145. randomTime -= 10
  146. showStr = f'剩余等待时间{randomTime}'
  147. if FunStatus is not None:
  148. FunStatus(showStr)
  149. print(showStr)
  150. time.sleep(randomTime)
  151. def myTimeSleep_small():
  152. time.sleep(random.randint(200, 1000) / 1000)
  153. def myTimeSleep_big():
  154. time.sleep(random.randint(1200, 2000) / 1000)
  155. def get_random_point(region):
  156. left, top, width, height = region
  157. x = random.randint(left, left + width)
  158. y = random.randint(top, top + height)
  159. return x, y
  160. def get_yys_random_point(region):
  161. dstRegion = yys_getRegion(region, True)
  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 yys_dragAuto(positions):
  216. return dragAuto('Mumu模拟器12', positions)
  217. def getRegion(gamestr, region, isWidth):
  218. tmpRet, tmpPos = find_window_pos(gamestr)
  219. if tmpRet == False:
  220. return False
  221. if isWidth:
  222. return (region[0] + tmpPos[0], region[1] + tmpPos[1], region[2], region[3])
  223. else:
  224. return (region[0] + tmpPos[0], region[1] + tmpPos[1], region[2] + tmpPos[0], region[3] + tmpPos[1])
  225. def game_region():
  226. tmpRet, tmpPos = find_window_pos('Mumu模拟器12',)
  227. return tmpRet, tmpPos
  228. def yys_getRegion(region, isWidth= False):
  229. return getRegion('Mumu模拟器12', region, isWidth)
  230. def click_waitimg(pos_x,pos_y,img):
  231. retry_times = 3
  232. while retry_times > 0 and waitFindImg_withBool(img,0.4,1) == False:
  233. pcacc_mouse.click(pos_x,pos_y)
  234. retry_times-=1
  235. return retry_times > 0