python实现图像识别功能

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现图像识别的具体代码,供大家参考,具体内容如下

#! /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废弃... 
''' 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python中的条件判断语句

详解Python中的条件判断语句

 一个else语句可以使用if语句结合起来。如果在if语句中的条件表达式解析为0或false值,那么else语句包含代码执行。 else语句是可选的声明,并if语句下面最多只有...

通过cmd进入python的实例操作

通过cmd进入python的实例操作

通过cmd启动Python需要先设置系统环境,设置步骤如下: 1、首先,在桌面找到 “计算机” 右键 找到 “属性”或者按下 win 键 再右键“计算机” 找到 “属性”也可以。如下图所...

python在windows下实现ping操作并接收返回信息的方法

本文实例讲述了python在windows下实现ping操作并接收返回信息的方法。分享给大家供大家参考。具体分析如下: 这段python代码调用windows下的ping命令,通过sub...

详解Python中打乱列表顺序random.shuffle()的使用方法

之前自己一直使用random中 randint生成随机数以及使用for将列表中的数据遍历一次。 现在有个需求需要将列表的次序打乱,或者也可以这样理解: 【需求】将一个容器中的数据每次...

python3-flask-3将信息写入日志的实操方法

使用logging模块,记录日志信息 安装模块 pip3 install logging 脚本示例 vim flask_api_logging.py #!/usr/bin/e...