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

yipeiwu_com6年前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设计】。

相关文章

解决python3 安装完Pycurl在import pycurl时报错的问题

此次遇到的问题是在import pycurl 时报错 pycurl:libcurl link-time version is older than compile-time versi...

基于python中staticmethod和classmethod的区别(详解)

例子 class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classm...

PyTorch 对应点相乘、矩阵相乘实例

一,对应点相乘,x.mul(y) ,即点乘操作,点乘不求和操作,又可以叫作Hadamard product;点乘再求和,即为卷积 data = [[1,2], [3,4], [5,...

解决Django删除migrations文件夹中的文件后出现的异常问题

migrate文件记录了每一次数据迁移的改变 解决方法:重建数据库 1.删除数据库 错误方法: python manage.py shell from app.models impo...

python实现字符串和日期相互转换的方法

本文实例讲述了python实现字符串和日期相互转换的方法。分享给大家供大家参考。具体分析如下: 这里用的分别是time和datetime函数 ''' @author: jiangqh...