使用pytesseract提取图片文字

效果展示:

(原始图片)

(运行结果)

 代码示例:

# -*- coding:utf-8 -*-
from PIL import Image
import pytesseract


def cleanFile(filePath, newFilePath):
    image = Image.open(filePath)

    # 对图片进行阈值过滤(低于143的置为黑色,否则为白色)
    # 相当于对电脑显卡调节对比度(电脑显卡对比度默认为50,我比较习惯于调成53)
    image = image.point(lambda x: 0 if x < 143 else 255)
    # 重新保存图片
    image.save(newFilePath)

    image = Image.open(newFilePath)
    text = pytesseract.image_to_string(image, lang='chi_sim')
    print(text)


if __name__ == "__main__":
    url = r"D:图片励志图片666.png"
    new_url = r"D:图片励志图片777.png"
    cleanFile(url, new_url)
原文地址:https://www.cnblogs.com/shun7man/p/14425931.html