图片转文字

图片转文字

有时候需要识别图片中的文字,把图片转成文本。方法有很多种,常用的是OCR。
以下是几个示例。


# 需要事前安装 easyocr, PIL
import easyocr
from PIL import Image
import numpy as np

def extract_text_from_png(png_file_path):
    image = Image.open(png_file_path)
    image_array = np.array(image)
    # reader = easyocr.Reader(['ch_sim'], gpu=False)  # 指定中文语言
    reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)  # 指定多种语言
    result = reader.readtext(image_array)

    # text = ' '.join([item[1] for item in result]) # 连接成字符串
    text = '\n'.join([item[1] for item in result]) # 换行
    return text

def main():
    png_file_path = 'your_file_path/example.PNG'

    try:
        text = extract_text_from_png(png_file_path)
    except Exception as e:
        print('Error: ', e)

    print(text)

if __name__ == '__main__':
    main()


# 需要事前安装 easyocr, PIL
import easyocr
from PIL import Image

def extract_text_from_png(png_file_path):
    reader = easyocr.Reader(['en'])  # 指定语言为英文,你可以根据需要选择其他语言
    # reader = easyocr.Reader(['ch_sim'], gpu=False)  # 指定中文语言
    result = reader.readtext(png_file_path)

    # text = ' '.join([item[1] for item in result]) # 连接成字符串
    text = '\n'.join([item[1] for item in result]) # 换行
    return text

def main():
    png_file_path = 'your_file_path/example.PNG'

    try:
        text = extract_text_from_png(png_file_path)
    except Exception as e:
        print('Error: ', e)

    print(text)

if __name__ == '__main__':
    main()


# 需要事前安装 pytesseract, PIL
import pytesseract
from PIL import Image

def extract_text_from_png(png_file_path):
    image = Image.open(png_file_path)
    text = pytesseract.image_to_string(image)
    return text

def main():
    png_file_path = 'your_file_path/example.PNG'

    try:
        text = extract_text_from_png(png_file_path)
    except Exception as e:
        print('Error: ', e)

    print(text)

if __name__ == '__main__':
    main()

注:
在Windows上安装Tesseract OCR引擎,你可以按照以下步骤进行操作:

  1. 访问Tesseract OCR的GitHub页面:https://github.com/tesseract-ocr/tesseract
  2. 在页面上找到并点击"Releases"选项卡。
  3. 在"Latest release"部分,找到适用于Windows的安装程序(.exe文件),并点击下载。
  4. 下载完成后,运行安装程序。按照提示进行安装,选择合适的安装路径和选项。
  5. 完成安装后,你需要将Tesseract OCR的安装路径添加到系统的环境变量中。打开"控制面板",搜索并打开"系统"。
  6. 在系统窗口中,点击"高级系统设置"。
  7. 在系统属性窗口中,点击"环境变量"按钮。
  8. 在"系统变量"部分,找到名为"Path"的变量,双击打开。
  9. 在"编辑环境变量"窗口中,点击"新建"按钮。
  10. 输入Tesseract OCR的安装路径,例如:“C:/Program Files/Tesseract-OCR”,然后点击"确定"。
  11. 确保所有窗口都已关闭,并重新启动你的计算机。
  12. 安装完成后,你可以在Python中使用pytesseract库来进行OCR文字识别。

请注意,以上步骤是基于Tesseract OCR的官方安装程序的安装过程。如果你选择了其他方式进行安装,可能会有所不同。确保按照你所选择的方式进行安装,并将安装路径正确地添加到系统的环境变量中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值