#直接参考官方SDK文档
def get_file_content(filePath):
with open(filePath, ‘rb’) as fp:
return fp.read()
def screenshots():
hwnd = win32gui.FindWindow(‘WeChatMainWndForPC’, ‘微信’)
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
pix = screen.grabWindow(hwnd).toImage()
pix.save(“123.jpg”)
if name == ‘main’:
screenshots()
# 批量处理图片
localdir = os.path.split(os.path.realpath(sys.argv[0]))[0] # 默认获取本程序所在的目录
allfilenames = os.listdir(localdir) # 获取该目录下所有文件
for filenames in allfilenames:
if os.path.splitext(filenames)[1] in [‘.jpg’,‘.png’]:
picabsolutepath = localdir + ‘\’ + filenames
image = get_file_content(picabsolutepath) # 获取图片
msg = client.basicGeneral(image) # 调用API解析图片后生成一段信息储存在变量中
for i in msg.get(‘words_result’): # 生成的msg实际是一个字典加列表的混合体,此处我们只需要用到字符串
print(i.get(‘words’))