将图像翻译成文字称为光学文字识别(Optical Character Recognition,OCR)
OCR库
Pillow,Tesseract,NumPy.
利用Pillow库,创建一个过滤器来去掉渐变的背景色,只把文字留下来。
from PIL import Image
import subprocess
def cleanFile(filePath,newFilePath):
image = Image.open(filePath)
#对图片进行过滤,然后保存
image = image.point(lamba x:0 if x<143 else 255)
image.save(newFilePath)
#调用系统的tesseract命令对图片进行OCR识别
subprecess.call(['tesseract',newFilePath,'output'])
#打开文件读取结果
outputFile = open('output.txt','r')
print(outputFile.read())
outputFile.close()
cleanFile('text_2,jpg','text_2_clean.png')
“`