If the strong standard library is the foundation of Python’s development, the rich third-party library is the guarantee of Python’s continuous development. Today’s Python tutorial will use the Itchart library to implement some operations on the computer through wechat.
Install itchat
Itchat is an open source wechat account interface that can be used to call wechat easily through Python.
pip3 install itchat
Copy the code
Install the OS
OS module is a module commonly used in operation and maintenance work. You can call system commands through the OS module
pip3 install os
Copy the code
Install cv2
Through CV2 to call the camera for image acquisition and other operations
pip3 install opencv-python
Copy the code
import itchat
import os
import cv2
The message type is itchat.content.text
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
print(msg)
global flag
# Send content
message = msg['Text']
# the receiver
toName = msg['ToUserName']
if toName == "filehelper":
if message == "pic":
# 0 stands for built-in camera 1, 2... To the outside world is
cap = cv2.VideoCapture(0)
ret, img = cap.read()
cv2.imwrite("pic.jpg", img)
# Send pictures to file Transfer Assistant
itchat.send('@img@%s' % u'pic.jpg'.'filehelper')
# Release the camera
cap.release()
if message[0:3] == "cmd":
# Execute the input command
os.system(message.strip(message[0:4]))
if __name__ == '__main__':
message =Instructions: 1. Enter [CMD XXX] to run the command. 2. Enter PIC to open camera"
"""So in auto_login() I'm going to give you a True, hotReload=True so that you can stay logged in even if you close the program and restart it within a certain amount of time so that you don't have to scan again."""
itchat.auto_login(True)
itchat.send(message, "filehelper")
itchat.run()
Copy the code
Accidentally exposed a selfie
Python’s powerful library makes development easy and fast. We can use itchat library to develop wechat robots, group messaging, user list analysis, reply messages and other functions. It is common to operate files, configuration files, and paths through OS libraries in automated operations. Cv2 can be used to collect images and videos, which are widely used in image recognition, motion tracking, machine vision and other fields
More Python tutorials and practical tips will continue to be shared.