comon.py 8.9 KB

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