comon.py 9.4 KB

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