• 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

  • 相关阅读:
    货币
    沙漏
    秋季学习总结
    三个老师
    介绍自己
    redis 的部分配置
    第二次博客作业
    第一次阅读作业
    shell_通配符
    shell_/dev/null,>和&
  • 原文地址:https://www.cnblogs.com/CoolJayson/p/7352129.html
Copyright © 2020-2023  润新知