python图片二值化提高识别率代码实例

yipeiwu_com5年前Python基础

这篇文章主要介绍了python图片二值化提高识别率代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

import cv2from PIL import Imagefrom pytesseract import pytesseractfrom PIL import ImageEnhanceimport reimport string
def createFile(filePath,newFilePath):

  img = Image.open(filePath)

  # 模式L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。
  Img = img.convert('L')
  Img.save(newFilePath)

  # 自定义灰度界限,大于这个值为黑色,小于这个值为白色
  threshold = 200

  table = []
  for i in range(256):
    if i < threshold:
      table.append(0)
    else:
      table.append(1)

  # 图片二值化
  photo = Img.point(table, '1')
  photo.save(newFilePath)
if __name__ == '__main__':
createFile(r'1.bmp',r'newTest.png')

原图:

处理过后的图:

识别结果:

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

相关文章

彻彻底底地理解Python中的编码问题

Python处理文本的功能非常强大,但是如果是初学者,没有搞清楚python中的编码机制,也经常会遇到乱码或者decode error。本文的目的是简明扼要地说明python的编码机制,...

Python标准库urllib2的一些使用细节总结

Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库。这里总结了一些 urllib2 的使用细...

python给微信好友定时推送消息的示例

如下所示: from __future__ import unicode_literals from threading import Timer from wxpy import...

打开电脑上的QQ的python代码

复制代码 代码如下:# _*_ coding:utf-8 _*_# name start_qq.pyimport osos.startfile("C:\Program Files\Ten...

python实现将excel文件转化成CSV格式

如下所示: import pandas as pd data = pd.read_excel('123.xls','Sheet1',index_col=0) data.to_csv(...