python -- 图像识别练习

本文介绍如何利用Python和Tesseract OCR工具进行光学字符识别(OCR),包括英文及中文文本的识别流程,并展示了如何通过命令行调用Tesseract识别图片中的文字。

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

#! /usr/bin/env python

from PIL import Image
import pytesseract

url='img/denggao.jpeg'
image=Image.open(url)
#image=image.convert('RGB') # RGB
image=image.convert('L') # 灰度
image.load()
text=pytesseract.image_to_string(image)
print text
#image.show()

r'''#
zhongwen_url = 'img/zhongwen003.png'
import os
fn = "aaaa"
# sudo apt-get install tesseract
cmd = "tesseract " + zhongwen_url + " " + fn + " -l chi_sim"
os.system(cmd)

with open(fn+".txt", "r") as f:
	print f


ret=os.system('cat /etc/pam.conf')
print ret
print '----------------------'
ret=os.popen('cat /etc/pam.conf')
print ret'''

r'''
import os
import subprocess

def image_to_string(img, cleanup=True, plus=''):
    # cleanup为True则识别完成后删除生成的文本文件
    # plus参数为给tesseract的附加高级参数
    subprocess.check_output('tesseract ' + img + ' ' +
                            img + ' ' + plus, shell=True)  # 生成同名txt文件
    text = ''
    with open(img + '.txt', 'r') as f:
        text = f.read().strip()
    if cleanup:
        os.remove(img + '.txt')
    return text
# run >>>
# print(image_to_string('./phototest.tif'))  # 打印识别出的文本,删除txt文件
# print(image_to_string('./phototest.tif', False))  # 打印识别出的文本,不删除txt文件
# print(image_to_string('./phototest.tif', False, '-l eng'))  # 打印识别出的文本,不删除txt文件,同时提供高级参数

# PyTesser废弃...
'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值