1. 使用OCR工具
OCR(光学字符识别)技术可以将图片中的文字提取出来。以下是几种常见的OCR工具:
Google Keep:将图片上传到Google Keep,然后使用“提取文字”功能。
Microsoft OneNote:将图片插入到OneNote中,右键点击图片并选择“复制图片中的文本”。
Adobe Acrobat:使用Adobe Acrobat的OCR功能将图片转换为可编辑的PDF或文本文件。
2. 在线OCR服务
有许多在线OCR服务可以将图片转换为文字,例如:
Online OCR(https://www.onlineocr.net/)
Smallpdf(https://smallpdf.com/ocr)
i2OCR(https://www.i2ocr.com/)
3. 手机应用
许多手机应用也提供图片转文字的功能,例如:
Google Lens:使用Google Lens拍摄或上传图片,然后提取文字。
Microsoft Office Lens:拍摄图片并提取文字,支持导出到Word、PDF等格式。
CamScanner:扫描图片并提取文字。
代码实现:
安装包
pip install paddlepaddle
pip install paddleocr
代码如下:
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=False, lang='ch', use_gpu=False)
image_path = 'test.png' # 替换为你的图片路径
result = ocr.ocr(image_path, cls=True)
print("OCR 结果原始数据:", result) # 🔴 调试输出
if not result or not result[0]:
print("未检测到文本")
else:
result = result[0] # 取出真正的 OCR 结果
with open('ocr_result.txt', 'w', encoding='utf-8') as f:
for idx, line in enumerate(result):
print(f"当前处理数据:{line}") # 🔴 调试输出
if len(line) < 2 or not isinstance(line[1], tuple):
print(f"⚠️ 第 {idx + 1} 行格式异常,跳过: {line}")
continue
text, confidence = line[1] # 取出文字和置信度
if not isinstance(text, str):
print(f"⚠️ 文字内容异常,跳过: {text}")
continue
f.write(text + '\n') # 确保正确写入
print(f"✅ 成功写入: {text}") # 🔴 调试输出
print("🎉 识别结果已保存到 ocr_result.txt")
示例图片:
识别效果:
ocr_result.txt
这个效果还是不错的