图片文字识别:Tesseract OCR库在Python中基本使用

本文介绍如何使用 Python 的 Tesseract OCR 库进行图片文字识别,包括基本使用方法及通过 Selenium 获取网页截图并识别其中文字的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片识别: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 ---------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值