在 Windows 下,使用 pyautogui 库来获取屏幕截图,相比于使用 Pillow中的 ImageGrab,更加迅捷。pyautogui 相比于 PyUserInput,依赖的库更少,便于配置。
import time
import numpy as np
# from PIL import ImageGrab
# from PIL import Image
import pyautogui
def click(i,j):
for cnt in range(12):
pyautogui.click(i, j)
time.sleep(0.05)
def scroll():
pyautogui.scroll(-100)
time.sleep(0.1)
pyautogui.scroll(-200)
time.sleep(0.1)
pyautogui.scroll(-10)
time.sleep(0.1)
def round():
# imgfn = ImageGrab.grab()
# imgnp = np.array(imgfn.getdata(), np.uint8).reshape(imgfn.size[1], imgfn.size[0], 3)
# img = Image.fromarray(imgnp).convert("RGB")
img = pyautogui.screenshot()
print(img.size)
width = img.size[0]#长度
height = img.size[1]#宽度
h = 0
while h < height:#遍历所有长度的点
w = 0
while w < width:#遍历所有宽度的点
data = img.getpixel((w,h))#i,j表示像素点
if (data[0]==187 and data[1]==187 and data[2]==187):
print("Checking",w,h)
data = img.getpixel((w+1,h))
if not (data[0]==187 and data[1]==187 and data[2]==187):
#print(data)
w = w+1
continue
cnt = 1
while cnt < 10:
data = img.getpixel((w,h+cnt))
if (data[0]==187 and data[1]==187 and data[2]==187):
cnt = cnt+1
else:
break
if cnt==10:
print("Clicking",w,h)
click(w,h)
h=h+10
break
w = w+1
h=h+1
for i in range(10):
print("Round",i)
round()
scroll()