使用OCR从多个图像中提取文本到CSV



我想从数千张图像中提取文本并将其放入CSV文件中。有人能告诉我怎么做吗?我的桌面保存了一些图片。

可以。

使用以下命令安装pytesseract模块:

pip install pytesseract

安装tesseract引擎可执行文件

  • tesseract v5.0.0 cmd 32 bit
  • tesseract v5.0.0 cmd 64位
  • 或按此查阅最新版本

创建一个名为images_to_csv.py的python脚本并粘贴以下代码:

import pytesseract
from PIL import Image # pip install Pillow
# set tesseract cmd to the be the path to your tesseract engine executable 
# (where you installed tesseract from above urls)
# IMPORTANT: this should end with '...tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = <path_to_your_tesseract_cmd>
# and start doing it
# your saved images on desktop
list_with_many_images = [
"path1",
"path2"
# ...
"pathN"
]
# create a function that returns the text
def image_to_str(path):
""" return a string from image """
return pytesseract.image_to_string(Image.open(path))
# now pure action + csv part
with open("images_content.csv", "w+", encoding="utf-8") as file:
file.write("ImagePath, ImageText")
for image_path in list_with_many_images:
text = image_to_str(image_path)
line = f"{image_path}, {text}n"
file.write(line)

这只是开始。

如果您想使用模块csv,请继续。

最新更新