屏幕處理
獲取屏幕截圖
我們控制鼠標的操作,不能盲目的進行,所以我們需要監控屏幕上的內容,從而決定要不要進行對應的操作, pyautogui 提供了一個方法screenshot(),可以返回一個Pillow的image對象;
這里有三個常用函數:
im = pyautogui.screenshot():返回屏幕的截圖,是一個Pillow的image對象 im.getpixel((500, 500)):返回im對象上,(500,500)這一點像素的顏色,是一個RGB元組 pyautogui.pixelMatchesColor(500,500,(12,120,400)) :是一個對比函數,對比的是屏幕上(500,500)這一點像素的顏色,與所給的元素是否相同;
im = pyautogui.screenshot() im.save('屏幕截圖.png')
保存屏幕截圖;
識別圖像
首先,我們需要先獲得一個屏幕快照,例如我們想要點贊,我們就先把大拇指的圖片保存下來;然后使用函數:locateOnScreen(‘zan.png’) ,如果可以找到圖片,則返回圖片的位置,如:Box(left=25, top=703, width=22, height=22);如果找不到圖片,則返回None;
如果,屏幕上有多處圖片可以匹配,則需要使用locateAllOnScreen(‘zan.png’) ,如果匹配到多個值,則返回一個list,參考如下:
import pyautogui pyautogui.PAUSE = 1 # 圖像識別(一個) btm = pyautogui.locateOnScreen('zan.png') print(btm) # Box(left=1280, top=344, width=22, height=22) # 圖像識別(多個) btm = pyautogui.locateAllOnScreen('zan.png') print(list(btm)) # [Box(left=1280, top=344, width=22, height=22), Box(left=25, top=594, width=22, height=22)]
pyautogui.center((left, top, width, height))返回指定位置的中心點;這樣,我們就可以再配合鼠標操作點擊找到圖片的中心;參考實例:點贊
#!/usr/bin/env python # -*- coding: utf-8 -*- import pyautogui import time def zan(): time.sleep(0.5) # 等待 0.5 秒 left, top, width, height = pyautogui.locateOnScreen('zan.png') # 尋找 點贊圖片; center = pyautogui.center((left, top, width, height)) # 尋找 圖片的中心 pyautogui.click(center) # 點擊 print('點贊成功!') while True: if pyautogui.locateOnScreen('zan.png'): zan() # 調用點贊函數 else: pyautogui.scroll(-500) # 本頁沒有圖片后,滾動鼠標; print('沒有找到目標,屏幕下滾~')

import pyautogui pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png') # 截全屏並設置保存圖片的位置和名稱 im = pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png') # 截全屏並設置保存圖片的位置和名稱 print(im) # 打印圖片的屬性 # 不截全屏,截取區域圖片。截取區域region參數為:左上角XY坐標值、寬度和高度 pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png', region=(0, 0, 300, 400)) pix = pyautogui.screenshot().getpixel((220, 200)) # 獲取坐標(220,200)所在屏幕點的RGB顏色 positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')' print(positionStr) # 打印結果為RGB:( 60, 63, 65) pix = pyautogui.pixel(220, 200) # 獲取坐標(220,200)所在屏幕點的RGB顏色與上面三行代碼作用一樣 positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')' print(positionStr) # 打印結果為RGB:( 60, 63, 65) # 如果你只是要檢驗一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函數,把X、Y和RGB元組值穿入即可 # 如果所在屏幕中(x,y)點的實際RGB三色與函數中的RGB一樣就會返回True,否則返回False # tolerance參數可以指定紅、綠、藍3種顏色誤差范圍 pyautogui.pixelMatchesColor(100, 200, (255, 255, 255)) pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10) # 獲得文件圖片在現在的屏幕上面的坐標,返回的是一個元組(top, left, width, height) # 如果截圖沒找到,pyautogui.locateOnScreen()函數返回None a = pyautogui.locateOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png') print(a) # 打印結果為Box(left=0, top=0, width=300, height=400) x, y = pyautogui.center(a) # 獲得文件圖片在現在的屏幕上面的中心坐標 print(x, y) # 打印結果為150 200 x, y = pyautogui.locateCenterOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png') # 這步與上面的四行代碼作用一樣 print(x, y) # 打印結果為150 200 # 匹配屏幕所有與目標圖片的對象,可以用for循環和list()輸出 pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png') for pos in pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png'): print(pos) # 打印結果為Box(left=0, top=0, width=300, height=400) a = list(pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')) print(a) # 打印結果為[Box(left=0, top=0, width=300, height=400)]

定位函數。都是從左上角原點開始向右向下搜索截圖位置: locateOnScreen(image, grayscale=False):返回找到的第一個截圖Image對象在屏幕上的坐標(left, top, width, height),如果沒找到返回None locateCenterOnScreen(image, grayscale=False):返回找到的第一個截圖Image對象在屏幕上的中心坐標(x, y),如果沒找到返回None locateAllOnScreen(image, grayscale=False):返回找到的所有相同截圖Image對象在屏幕上的坐標(left, top, width, height)的生成器 locate(needleImage, haystackImage, grayscale=False):返回找到的第一個截圖Image對象在haystackImage里面的坐標(left, top, width, height),如果沒找到返回None locateAll(needleImage, haystackImage, grayscale=False):返回找到的所有相同截圖Image對象在haystackImage里面的坐標(left, top, width, height)的生成器
提示信息框
提示框/警告框
import pyautogui a = pyautogui.alert(text='This is an alert box.', title='Test') print(a)
輸出如下圖:點擊確定,返回值為‘OK’
選擇框
import pyautogui a = pyautogui.confirm('選擇一項', buttons=['A', 'B', 'C']) print(a)
輸出如下圖:點擊B選項,返回值為‘B’
密碼輸入
import pyautogui a = pyautogui.password('Enter password (text will be hidden)') print(a)
輸出如下圖:輸入密碼,顯示為密文,點擊OK,返回值為剛剛輸入的值;
普通輸入
import pyautogui a = pyautogui.prompt('請輸入一個數字:') print(a)
輸出如下圖:顯示為明文,點擊OK,返回值為剛剛輸入的值。
灰度值匹配
可以把grayscale參數設置為True來加速定位(大約提升30%),默認為False。這種去色(desaturate)方法可以加速定位,但是也可能導致假陽性(false-positive)匹配:
import pyautogui button7location = pyautogui.locateOnScreen('pyautogui/calc7key.png', grayscale=True) button7location (1227, 546, 29, 28)
像素匹配
import pyautogui im = pyautogui.screenshot() im.getpixel((100, 200)) (255, 255, 255)
pyautogui.pixel(100, 200)
(255, 255, 255)
如果你只是要檢驗一下指定位置的像素值,可以用pixelMatchesColor()函數,把X、Y和RGB元組值穿入即可:
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255)) True pyautogui.pixelMatchesColor(100, 200, (255, 255, 245)) False
pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10) True pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10) True pyautogui.pixelMatchesColor(100, 200, (205, 255, 245), tolerance=10) False
輸入中文
pyperclip模塊
copy('str1') 復制內容str1,內容可設置為中文等
paste() 將復制的內容粘貼到輸入處,粘貼時也可使用pyautogui的hotkey實現
感覺沒啥用