zoukankan      html  css  js  c++  java
  • Tesseract处理背景渐变的图片

    在Tesseract处理背景渐变图片不太理想的情况下, 可以利用Pillow库, 创建一个阈值过滤器来去掉渐变的背景色, 只把文字留下来, 从而让图片更清晰, 便于Tesseract读取:

     1 from PIL import Image
     2 import subprocess
     3 
     4 def cleanFile(filePath, newFilePath):
     5     image = Image.open(filePath)
     6 
     7     # 对图片进行阈值过滤, 然后保存
     8     image = image.point(lambda x: 0 if x<143 else 255)
     9     image.save(newFilePath)
    10 
    11     # 调用系统的tesseract命令对图片进行ocr识别
    12     # subprocess.call(["tesseract", newFilePath, "output"])  //报错:文件找不到
    13     subprocess.call(["C:/Program Files (x86)/Tesseract-OCR/tesseract", newFilePath, "output"])
    14 
    15     # 打开文件读取结果
    16     outputFile = open("output.txt", 'r')
    17     print(outputFile.read())
    18     outputFile.close()
    19 
    20 cleanFile("text_2.png", "text_2_clean.png")

    以下两张图片分别为text_2.png和text_2_clean.png

  • 相关阅读:
    sklearn库学习笔记1——preprocessing库
    juypter notetbook
    信用卡欺诈
    matplotlib1
    python一行输入多个数
    pandas数据预处理
    pandas基础用法
    numpy简单用法2
    numpy 简单用法
    简单循环
  • 原文地址:https://www.cnblogs.com/CoolJayson/p/7352129.html
Copyright © 2011-2022 走看看