1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import win32gui,time from PIL import Image from PIL import ImageGrab from PIL import ImageChops hwnd=win32gui.FindWindow("#32770","大家来找茬") #找到窗口句柄 if hwnd: # print(hwnd) win32gui.SetForegroundWindow(hwnd) #激活窗口到最前 game_rect = win32gui.GetWindowRect(hwnd) #找到整个窗口矩形 time.sleep(0.1) # print(game_rect[0],game_rect[1],game_rect[2],game_rect[3]) #0为窗口左边上离原点的x距离,1为0为窗口左边上离原点的y距离,2为窗口右下离原点的x距离,3为窗口右下离原点的y距离
width = game_rect[2] - game_rect[0] #矩形的位置 height = game_rect[3] - game_rect[1] # print(width,height) base_width=1024 #原来宽度 base_height=768 #原来高度 rate_width=width/base_width #计算缩放比例 rate_height=height/base_height #计算缩放比例 # print(rate_width,rate_height) rect1 = (game_rect[0]+int(92*rate_width),game_rect[1]+int(312*rate_height),game_rect[0]+width/2-int(37*rate_width),game_rect[1]+int(598*rate_height)) #取四个值,截取。ImageGrab.grab((num, num, num, num)),指定范围内的图像 rect2 = (game_rect[0]+width/2+int(37*rate_width),game_rect[1]+int(312*rate_height),game_rect[2]-int(92*rate_width),game_rect[1]+int(598*rate_height)) #取四个值,截取。ImageGrab.grab((num, num, num, num)),指定范围内的图像 ima = ImageGrab.grab(rect1) #抓取当前屏幕内容 imb = ImageGrab.grab(rect2) out = ImageChops.difference(ima,imb) #返回两幅图像逐像素差的绝对值形成的图像。 # ima.save("test1.jpg",'jpeg') #保存的左图,测试用,可注释 # imb.save("test2.jpg",'jpeg') #保存的右图,测试用,可注释 out.save("out.jpg",'jpeg') #最后比较完的结果图 aaa=Image.open("out.jpg") aaa.show()
|