图片识别:Tesseract OCR库在Python中基本使用
一.Tesseract - Xmind的笔记
二. 代码案例:
基本使用代码
import pytesseract
from PIL import Image
# 创建图片对象
image = Image.open('test_image.png')
# 使用tesseract识别图片中的文字
print(pytesseract.image_to_string(image, config='-psm 7'))
猫眼评分-案例代码
from io import BytesIO
import time
from PIL import Image
import pytesseract
import requests
from selenium import webdriver
class MaoYan(object):
def __init__(self):
self.url = 'http://maoyan.com/films/1200486'
def run(self):
# 创建chrome对象,发送selenium请求,获取全屏对象
chrome = webdriver.Chrome('/home/python/Desktop/chromedriver')
chrome.implicitly_wait(5) # 设置浏览器隐式等待 页面加载
chrome.get(self.url)
# 让背景变白色和文字变黑色,更易识别
chrome.execute_script('document.querySelector(".banner").style.background = "white"')
chrome.execute_script('document.querySelector(".stonefont").style.color = "black"')
screen_shot = chrome.get_screenshot_as_png() # 获取全屏截图的对象
screen_image = Image.open(BytesIO(screen_shot)) # 以IO的形式转换为二进制,创建图片对象
screen_image.save('15_screen_image.png')
# 获取评分元素对象,计算评分元素的位置区域信息,截取评分图片
el_score = chrome.find_element_by_xpath('//span[@class="index-left info-num "]/span[@class="stonefont"]')
height = el_score.size['height']
width = el_score.size['width']
left = el_score.location['x']
top = el_score.location['y']
right = left + width
bottom = top + height
cut_info = (left, top, right, bottom)
print(cut_info)
cut_image = screen_image.crop(cut_info)
cut_image.save('15_cut_image.png')
# 使用tesseract库,进行文字识别
try:
score = pytesseract.image_to_string('15_cut_image.png', config='-psm 7')
print(score)
except Exception as e:
print(e)
print('识别失败')
if __name__ == '__main__':
mao_yan = MaoYan()
mao_yan.run()
----------------------------- END ---------------------------------------