We need to pyautogui modular ,pyautogui It's pure. Python Of GUI Automation tools , It allows the program to automatically control the mouse and keyboard .
1、 install
pip3 install pyautogui
After installation, it can be used
2、 How to find the icon of the circle of friends on the screen
First take a screenshot of the circle of friends icon , Name it ( Circle of friends .png),pyautogui Use the function to match on the screen , Until we find an icon that is the same as our target icon , And you get the result .
# Image recognition ( One )
oneicon = pyautogui.locateOnScreen(' Circle of friends .png')
Identify the result of an icon
Identify two points
# Image recognition ( Multiple )
multicon = pyautogui.locateAllOnScreen(' Two points .png')
After two points are identified, a result similar to the following is returned ( A list of ):
[Box(left=985, top=344, width=79, height=49), Box(left=985, top=1322, width=79, height=49)]
This is it. “ Two points ”( On the screenshot are two ) On the desktop , If you can't find the picture , It will return None.
Find the location of the picture and click , Just ok 了 .
pyautogui.click( Location )
3、 Program
import pyautogui
import time
top = 0 # The value that the screen scrolls up , It can be adjusted according to your own screen
covertop = 500 # The height of the cover of the circle of friends
delay = 0.01
# Looking for pictures
def findimge(image):
time.sleep(delay) # Time delay
if pyautogui.locateOnScreen(image):
left, top, width, height = pyautogui.locateOnScreen(image)
point = pyautogui.center((left, top, width, height))
pyautogui.click(point)
# Find the position of two points Roll one top value
def findtwopoint():
global top
time.sleep(delay)
if pyautogui.locateOnScreen(' Two points .png'):
left, top, width, height = pyautogui.locateOnScreen(' Two points .png')
point = pyautogui.center((left, top, width, height))
pyautogui.click(point)
def scrool():
global top
global covertop
time.sleep(delay)
if covertop == 0:
pyautogui.scroll(int(-top / 2)) # Scroll the mouse wheel
else:
pyautogui.scroll(-top + covertop)
covertop = 0
if __name__ == "__main__":
findimge(' Circle of friends .png')
while True:
findtwopoint()
findimge(' Fabulous .png')
scrool()
4、 function :
Open the computer version of wechat , window maximizing
Run the program
The problem is : Like it , If you click again on the current screen , If you can detect that you have clicked , Just roll over ok 了 .