python之PyAutoGui教你做個自動腳本計算器的方法
前提環(huán)境準(zhǔn)備
python3+pillow+pyautogui
先提前安裝好python3以及pillow和pyautogui模塊
這里介紹一下模塊安裝方法
pip install pillow pip install pyautogui pip install opencv-python
最終效果是利用python腳本模擬電腦計算器進(jìn)行自動計算,相當(dāng)于模擬人去點(diǎn)擊自帶的計算器進(jìn)行運(yùn)算,想要做到這一點(diǎn)需要有兩個條件:
1.模擬鼠標(biāo)和鍵盤的輸入工作
2.識別計算器按鈕的位置
先來看一下win10電腦的計算器是什么樣子的:
我們要知道一點(diǎn),計算器窗口的位置每次都是不同的,如果你是固定去確定按鈕的坐標(biāo)那就太被動了,所以我們這里需要用到圖像識別,去識別到按鈕的位置,博主這里
做一個示例 做一個1+2=的運(yùn)算。
廢話不多說直接上代碼,跟著注釋,看懂代碼沒毛病。
打開你的微信截圖截下1,+,2,=四個圖片存入腳本所在目錄
詳細(xì)代碼
#導(dǎo)入模塊 from PIL import ImageGrab import pyautogui as auto #定義類 class Screenshoot: def __init__(self): #self.bbox = bbox #self.name = name #self.im = ImageGrab.grab(self.bbox) #定位xy坐標(biāo),confidence為相似度判斷,最好不要使用1.0完全相似,比較容易不識別 self.position_1 = auto.locateCenterOnScreen('1.png', confidence=0.9) self.position_2 = auto.locateCenterOnScreen('2.png', confidence=0.9) self.position_3 = auto.locateCenterOnScreen('+.png', confidence=0.9) self.position_4 = auto.locateCenterOnScreen('=.png', confidence=0.9) pass def fullshoot(self): #全屏截圖 #self.im.save('01.png') pass def partialshoot(self): #局部精確截圖 #self.im.save(self.name+'.png') pass def position_show(self): #打印各坐標(biāo) print(self.position_1) print(self.position_2) print(self.position_3) print(self.position_4) def caculate(self): #依次點(diǎn)擊按鈕 auto.click(self.position_1) auto.click(self.position_3) auto.click(self.position_2) auto.click(self.position_4) #對象初始化 shoot1 = Screenshoot() #對象函數(shù)執(zhí)行 shoot1.position_show() shoot1.caculate() #shoot1.partialshoot() #shoot1.fullshoot()
運(yùn)行結(jié)果
到此這篇關(guān)于python之PyAutoGui教你做個自動腳本計算器的方法的文章就介紹到這了,更多相關(guān)PyAutoGui 自動腳本計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python word文本自動化操作實現(xiàn)方法解析
- Python自動化操作實現(xiàn)圖例繪制
- 利用Python自動化操作AutoCAD的實現(xiàn)
- Python利用splinter實現(xiàn)瀏覽器自動化操作方法
- 教你怎么用python selenium實現(xiàn)自動化測試
- 使用Gitee自動化部署python腳本的詳細(xì)過程
- python 辦公自動化——基于pyqt5和openpyxl統(tǒng)計符合要求的名單
- Python辦公自動化之Excel(中)
- python PyAUtoGUI庫實現(xiàn)自動化控制鼠標(biāo)鍵盤
- Python鍵鼠操作自動化庫PyAutoGUI簡介(小結(jié))
- python 利用PyAutoGUI快速構(gòu)建自動化操作腳本
相關(guān)文章
python自動化測試中APScheduler?Flask的應(yīng)用示例
這篇文章主要為大家介紹了python自動化測試中APScheduler?Flask的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07