from PIL import ImageGrab import pyautogui import time import pyperclip import numpy def 获取给定区域内的符合颜色值的第一个和最后一个坐标(left_x:int,left_y:int,right_x:int,right_y:int,color_r:int,color_g:int,color_b:int)->list: ''' 注意,本函数直接截取当前屏幕的全屏画面,然后再根据给定范围来查询。 left_x,left_y:给定区域内的左上角坐标 right_x,right_y:给定区域内的右下角坐标 color_r,color_g,color_b:以RGB模式表示的颜色值的R/G/B值 返回list,若是没有找到,将返回[(-1,-1),(-1,-1)] ''' key_name = "{}|{}|{}".format(color_r,color_g,color_b) color_dict={key_name:[]} pim = ImageGrab.grab() im = numpy.array(pim) y,x = numpy.where(numpy.all(im==[color_r,color_g,color_b],axis=2)) result_xy =zip(x,y) result_xy_list = [(x_,y_) for x_,y_ in result_xy if (x_>=left_x and x_ <=right_x)and(y_>=left_y and y_<=right_y)] # print(len(result_xy_list)) if(len(result_xy_list)>0): return [result_xy_list[0],result_xy_list[-1]] #下面的方法比较慢,但是不是不能用 # for x in range(left_x,right_x): # for y in range(left_y,right_y): # color = pim.getpixel((x,y)) # color_key = "{}|{}|{}".format(color[0],color[1],color[2]) # if(color_key==key_name): # color_dict[color_key].append((x,y)) # color_dict[key_name].sort(key=lambda x:(x[1])) # if(len(color_dict[key_name])>0): # return [color_dict[key_name][0],color_dict[key_name][-1]] else: return [(-1,-1),(-1,-1)]; def main(): # 屏幕分辨率为3200*1800 # 浏览器关闭垂直标签,处于右半边屏幕 #移动鼠标到空白处:2246,127 ,然后左键单击一下。 pyautogui.click(x=2246,y=127) pyautogui.hotkey('ctrl', 's') time.sleep(2) pyautogui.press('enter') time.sleep(0.2) pyautogui.hotkey('alt', 'y') time.sleep(2) pyautogui.hotkey('ctrl', 'home') #移动鼠标到空白处:2246,127,然后左键单击一下,避免浏览器下载窗口弹出的影响 pyautogui.click(x=2246,y=127) #移动到指定点,然后按下鼠标左键;复制标题 pyautogui.moveTo(x=1965,y=397) pyautogui.mouseDown() time.sleep(1) pyautogui.moveTo(x=3175,y=397) pyautogui.mouseUp() pyautogui.hotkey('ctrl', 'c') print("已保存:{}".format(pyperclip.paste())) pyautogui.hotkey('ctrl', 'f') time.sleep(0.2) pyautogui.hotkey('ctrl', 'v') time.sleep(1) pyautogui.hotkey('ctrl', 'end') time.sleep(0.2) point_xy=None found_num=0 while True: point_xy = 获取给定区域内的符合颜色值的第一个和最后一个坐标(1605,100,1954,1530,255,255,0) if(point_xy[0][0]==-1): point_xy = 获取给定区域内的符合颜色值的第一个和最后一个坐标(1605,100,1954,1530,255,150,50) if(point_xy[0][0]==-1): if found_num >=10: input("没有找到对应位置,请处理。处理完成请随意在控制台输入一个字符。") found_num = 0 else: break else: break if(found_num % 4 == 0): #向下概率更大 pyautogui.click(x=1940,y=195) else: pyautogui.click(x=1940,y=1590) # time.sleep(0.2) # pyautogui.click(x=1940,y=1590) found_num += 1 #一行文字的高度是17,然后再向右便宜24像素 click_point_x = point_xy[0][0] + 24 click_point_y = point_xy[1][1] + 17 print("点击({},{})".format(click_point_x,click_point_y)) pyautogui.click(x=click_point_x,y=click_point_y) time.sleep(2) if __name__ == "__main__": # print(获取给定区域内的符合颜色值的第一个和最后一个坐标(1605,341,1954,1513,255,255,0)) while True: main() time.sleep(2)
标签:pyautogui,point,color,获取,给定,坐标,key,xy,click From: https://www.cnblogs.com/love-DanDan/p/18166674