图片转文字
有时候需要识别图片中的文字,把图片转成文本。方法有很多种,常用的是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引擎,你可以按照以下步骤进行操作:
- 访问Tesseract OCR的GitHub页面:https://github.com/tesseract-ocr/tesseract
- 在页面上找到并点击"Releases"选项卡。
- 在"Latest release"部分,找到适用于Windows的安装程序(.exe文件),并点击下载。
- 下载完成后,运行安装程序。按照提示进行安装,选择合适的安装路径和选项。
- 完成安装后,你需要将Tesseract OCR的安装路径添加到系统的环境变量中。打开"控制面板",搜索并打开"系统"。
- 在系统窗口中,点击"高级系统设置"。
- 在系统属性窗口中,点击"环境变量"按钮。
- 在"系统变量"部分,找到名为"Path"的变量,双击打开。
- 在"编辑环境变量"窗口中,点击"新建"按钮。
- 输入Tesseract OCR的安装路径,例如:“C:/Program Files/Tesseract-OCR”,然后点击"确定"。
- 确保所有窗口都已关闭,并重新启动你的计算机。
- 安装完成后,你可以在Python中使用
pytesseract
库来进行OCR文字识别。
请注意,以上步骤是基于Tesseract OCR的官方安装程序的安装过程。如果你选择了其他方式进行安装,可能会有所不同。确保按照你所选择的方式进行安装,并将安装路径正确地添加到系统的环境变量中。