Image recognition based on Python

Image recognition based on Python

1. Take a screenshot

We control the operation of the mouse and cannot be carried out blindly, so we need to monitor the content on the screen to decide whether to perform the corresponding operation. pyautogui provides a method screenshot() that can return a Pillow image object;

There are three commonly used functions:

im = pyautogui.screenshot(): Returns the screenshot of the screen, which is a Pillow image object
im.getpixel((500, 500)): returns the color of the pixel at (500, 500) on the im object, which is an RGB tuple
pyautogui.pixelMatchesColor(500,500,(12,120,400)): it is a contrast function, and the comparison is on the screen (500, 500) Is the color of the pixel at this point the same as the given element;

im = pyautogui.screenshot()
im.save('屏幕截图.png')

Save the screenshot;

2. Recognize the image

First, we need to get a screenshot. For example, if we want to like, we first save the image of the thumb; then use the function: locateOnScreen('zan.png'), if the image can be found, return the location of the image , Such as: Box(left=25, top=703, width=22, height=22); if the picture cannot be found, it returns None;
if there are multiple pictures on the screen that can match, you need to use locateAllOnScreen('zan .png'), if multiple values ​​are matched, a list is returned, as follows:

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)) Return the center point of the specified position; in this way, we can click with the mouse to find the center of the picture;

3. Auto Like Program

We need to like all articles. After the likes on this page are finished, scroll the mouse to like all the newly loaded articles;

Insert picture description here
code show as below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Zhang Kai 

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('没有找到目标,屏幕下滚~')

After running, they will be
Insert picture description here
liked one by one: The input in Pycharm is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47139649/article/details/109266602