imgFind.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # -*- coding: utf-8 -*-
  2. import pyautogui
  3. import random
  4. import time
  5. import numpy as np
  6. import cv2
  7. from PIL import Image
  8. import pyscreeze
  9. pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = False
  10. def color_string_to_rgb(color_string):
  11. r = int(color_string[0:2], 16)
  12. g = int(color_string[2:4], 16)
  13. b = int(color_string[4:6], 16)
  14. return (r, g, b)
  15. def savePic(screenshot, fileName):
  16. image = Image.frombytes('RGB', screenshot.size, screenshot.bgra, 'raw', 'BGRX')
  17. image.save(fileName) # 保存为PNG格式的图像文件
  18. def find_center_point(pointArr):
  19. min_distance = float('inf')
  20. center_point = None
  21. for point in pointArr:
  22. total_distance = 0
  23. for other_point in pointArr:
  24. distance = ((other_point[0] - point[0]) ** 2 + (other_point[1] - point[1]) ** 2) ** 0.5
  25. total_distance += distance
  26. if total_distance < min_distance:
  27. min_distance = total_distance
  28. center_point = point
  29. return center_point
  30. class pcacc_img:
  31. def __init__(specifically):
  32. random.seed()
  33. @staticmethod
  34. def find_maxColor_position(cur_region, colorArr): #(left, top, width, height)
  35. left = cur_region[0]
  36. top = cur_region[1]
  37. width = cur_region[2]
  38. height = cur_region[3]
  39. region = (left, top, width, height)
  40. # 目标颜色和容差值
  41. tolerance = 10 # 容差值
  42. # 在指定区域获取屏幕图像
  43. screenshotSrc = pyautogui.screenshot(region=region)
  44. print(region)
  45. screenshot = np.array(screenshotSrc)
  46. dstPointArr = []
  47. # 创建颜色范围的上下界
  48. for color_str in colorArr:
  49. target_color = color_string_to_rgb(color_str)
  50. lower_color = np.array(target_color) - tolerance
  51. upper_color = np.array(target_color) + tolerance
  52. # 在图像中查找匹配的像素
  53. mask = cv2.inRange(screenshot, lower_color, upper_color)
  54. # 显示 mask 图像
  55. #plt.imshow(mask, cmap='gray')
  56. #plt.show()
  57. points = np.transpose(np.where(mask > 0))
  58. if len(points) < 5:
  59. continue
  60. # 转换坐标到全局坐标系
  61. points[:, 0] += region[0]
  62. points[:, 1] += region[1]
  63. '''
  64. # 使用K-means聚类将匹配点分为不同的簇
  65. n_clusters = 5 # 簇的数量,可根据需要进行调整
  66. kmeans = KMeans(n_clusters=n_clusters)
  67. kmeans.fit(points)
  68. # 找到最大的簇
  69. max_cluster_label = np.argmax(np.bincount(kmeans.labels_))
  70. max_cluster_points = points[kmeans.labels_ == max_cluster_label]
  71. '''
  72. # 输出最集中的像素区域的位置
  73. for point in points:
  74. dstPointArr.append(point)
  75. if len(dstPointArr) > 0:
  76. return True, find_center_point(dstPointArr)
  77. else:
  78. return False, (-1, -1)
  79. @staticmethod
  80. def find_img_position(image_path):
  81. # 设置查找的置信度(confidence)阈值,范围从0到1,默认为0.999
  82. confidence_threshold = 0.85
  83. # 查找模糊图片在屏幕上的位置
  84. position = pyautogui.locateOnScreen(image_path, confidence=confidence_threshold)
  85. if position is not None:
  86. return True, position
  87. else:
  88. return False, None
  89. @staticmethod
  90. def choose_two_point_from_position(position):
  91. dst_x1 = random.randint(position.left, position.left + position.width)
  92. dst_y1 = random.randint(position.top, position.top + position.height)
  93. dst_x2 = random.randint(position.left, position.left + position.width)
  94. dst_y2 = random.randint(position.top, position.top + position.height)
  95. return (dst_x1, dst_y1), (dst_x2, dst_y2)
  96. @staticmethod
  97. def choose_one_point_from_position(position):
  98. dst_x1 = random.randint(position.left, position.left + position.width)
  99. dst_y1 = random.randint(position.top, position.top + position.height)
  100. return (dst_x1, dst_y1)
  101. @staticmethod
  102. def find_all_img(image_path):
  103. # 设置查找的置信度(confidence)阈值,范围从0到1,默认为0.999
  104. confidence_threshold = 0.85
  105. # 查找模糊图片在屏幕上的位置
  106. positions = pyautogui.locateAllOnScreen(image_path, confidence=confidence_threshold)
  107. if len(positions) == 0:
  108. return False, []
  109. return True, positions
  110. @staticmethod
  111. def find_img(image_path):
  112. # 设置查找的置信度(confidence)阈值,范围从0到1,默认为0.999
  113. confidence_threshold = 0.95
  114. # 查找模糊图片在屏幕上的位置
  115. position = pyautogui.locateOnScreen(image_path, confidence=confidence_threshold)
  116. if position is not None:
  117. # 图片找到了,获取图片的中心点坐标
  118. #mid
  119. mid_left = int(position.left + position.width / 4)
  120. mid_right = int(position.left + position.width / 4 * 3)
  121. mid_top = int(position.top + position.height / 4)
  122. mid_bottom = int(position.top + position.height / 4 * 3)
  123. #want_position
  124. want_x = random.randint(mid_left, mid_right)
  125. want_y = random.randint(mid_top, mid_bottom)
  126. #dst_position
  127. dst_x = random.randint(int(want_x - position.width / 3), int(want_x + position.width / 3))
  128. dst_y = random.randint(int(want_y - position.height / 3), int(want_y + position.height / 3))
  129. print(f"{image_path}:({dst_x}, {dst_y})")
  130. return True, (dst_x, dst_y)
  131. else:
  132. print(f"{image_path}图片未找到")
  133. return False, (-1, -1)
  134. @staticmethod
  135. def find_imgArr(imgArr):
  136. for cur_img in imgArr:
  137. ret, pos = pcacc_img.find_img(cur_img)
  138. if ret:
  139. return ret, pos
  140. return False, None
  141. @staticmethod
  142. def find_imgs(images):
  143. if isinstance(images, str):
  144. return pcacc_img.find_img(images)
  145. elif isinstance(images, (list, tuple)):
  146. return pcacc_img.find_imgArr(images)
  147. else:
  148. raise ValueError("Invalid input type for 'images' parameter.")
  149. @staticmethod
  150. def find_all_image_locations(image):
  151. confidence_threshold = 0.90
  152. # 查找所有符合条件的图片位置
  153. locations = pyautogui.locateAllOnScreen(image, confidence=confidence_threshold)
  154. # 将位置信息保存到列表中
  155. image_locations = []
  156. for location in locations:
  157. # 获取位置信息的左上角坐标和宽高
  158. x, y, width, height = location
  159. # 将位置信息添加到列表中
  160. image_locations.append((x, y, width, height))
  161. return image_locations
  162. @staticmethod
  163. def find_img_in_area(image,region=None):
  164. confidence_threshold = 0.85
  165. location = pyautogui.locateOnScreen(image,region=region, confidence=confidence_threshold)
  166. if location is not None:
  167. return True
  168. return False
  169. if __name__ == '__main__':
  170. time.sleep(2)
  171. begin = time.time()
  172. pcacc_img.find_maxColor_position((200, 200, 800, 200), {'2EA043'})
  173. cost = time.time() - begin
  174. print("函数执行耗时:", cost, "秒")