python - 提高 pyautogui.press 的速度
问题描述
我正在为 10fastfingers.com 编写一个自动打字机器人。有用。
我使用 pytesseract 库来查找文本,然后我使用 pyautogui 按键。我认识到 pyautogui.press 不是那么快。
我发现类似 pyautoguide.press 但速度很快。喜欢这个视频
from PIL import Image
import pyautogui
import time
import cv2
import pytesseract
class Cordinates():
textbox = (80, 275)
replayBtn = (800,450)
whereitype = (250,460)
def restartGame():
pyautogui.click(Cordinates.replayBtn)
def main():
restartGame()
time.sleep(0.5)
pyautogui.click(Cordinates.whereitype)
while True:
pyautogui.screenshot('image.png')
img = cv2.imread("image.png")
crop_image = img[320:370, 80:940]
cv2.imshow("cropped", crop_image)
cv2.imwrite('cropped.png',crop_image)
text = pytesseract.image_to_string(crop_image, lang='eng')
for c in text:
print(c)
if c == 'enter':
c=' '
pyautogui.press(c)
if pyautogui.position() == (1, 1):
exit()
pyautogui.press(' ')
main()
解决方案
查看脚本https://github.com/asweigart/pyautogui/blob/master/pyautogui/init .py
pyautogui.press
,默认情况下,在模块中全局使用 0.1 秒设置PAUSE
:
def press(keys, presses=1, interval=0.0, pause=None, logScreenshot=None, _pause=True):
"""Performs a keyboard key press down, followed by a release.
...
...
_autoPause(pause, _pause)
_自动暂停:
def _autoPause(pause, _pause):
"""If `pause` is not `None`, then sleep for `pause` seconds.
If `_pause` is `True`, then sleep for `PAUSE` seconds (the global pause setting).
This function is called at the end of all of PyAutoGUI's mouse and keyboard functions. Normally, `_pause`
is set to `True` to add a short sleep so that the user can engage the failsafe. By default, this sleep
is as long as `PAUSE` settings. However, this can be override by setting `pause`, in which case the sleep
is as long as `pause` seconds.
"""
if pause is not None:
time.sleep(pause)
elif _pause:
assert isinstance(PAUSE, int) or isinstance(PAUSE, float)
time.sleep(PAUSE)
和PAUSE
:
PAUSE = 0.1 # The number of seconds to pause after EVERY public function call. Useful for debugging.
如您所见,如果 pause inpress
是None
,默认情况下,它会恢复为 0.1 second PAUSE
。
您可以通过设置较低的暂停率来覆盖它。pyautogui.press(c, pause = 0.05)
推荐阅读
- c++ - 带 USB 集线器的 C++ 串行端口
- kubernetes - 是否可以将 pod 配置为优先使用“hostNetwork”但仍引用内部服务端点?
- html - 仅当 li 项中存在 dropend 时,如何从下拉菜单中删除边界右下角半径
- python - 将数据帧字典中的列转换为字符串
- chart.js - Chart.js - 向轴添加 1 个刻度步骤
- reactjs - React.js 无法使用 useEffect & fetch 在 UI 中显示 API 数据
- css - Material-UI Grid 同一行元素的高度
- javascript - 使用 fetch 后,离子角度组件未呈现
- linux - 使用 pip3 安装 pytorch 的问题:-f 选项需要 1 个参数
- javascript - 对于每个循环进入 URLFetchapp