Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用OCR将多个图像中的文本提取到CSV_Python_Image_Ocr_Screen Scraping_Tesseract - Fatal编程技术网

Python 使用OCR将多个图像中的文本提取到CSV

Python 使用OCR将多个图像中的文本提取到CSV,python,image,ocr,screen-scraping,tesseract,Python,Image,Ocr,Screen Scraping,Tesseract,我想从数千张图片中提取文本,并将其放入CSV文件中。谁能告诉我怎么做?我的桌面上保存了图像。当然可以 使用以下命令安装PyteSeract模块: pip install pytesseract 从以下URL安装tesseract引擎可执行文件: 或 创建一个名为images\u to_csv.py的python脚本,并粘贴以下代码: import pytesseract from PIL import Image # pip install Pillow # set tesseract

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

当然可以

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

pip install pytesseract
从以下URL安装tesseract引擎可执行文件:

创建一个名为
images\u 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)
导入pytesseract
从PIL导入图像#pip安装枕头
#将tesseract cmd设置为tesseract引擎可执行文件的路径
#(您从上述URL安装tesseract的位置)
#重要提示:应以“…\tesseract.exe”结尾
pytesseract.pytesseract.tesseract\u cmd=
#然后开始做
#您在桌面上保存的图像
列出带有多张图片的图片=[
“路径1”,
“路径2”
# ...
“路径”
]
#创建一个返回文本的函数
def图像_到_str(路径):
“”“从图像返回字符串”“”
将pytesseract.image_返回到_字符串(image.open(path))
#现在是纯动作+csv部分
打开(“images_content.csv”,“w+”,encoding=“utf-8”)作为文件:
file.write(“ImagePath,ImageText”)
对于包含多个图像的列表中的图像路径:
text=image\u to\u str(image\u路径)
line=f“{image\u path},{text}\n”
file.write(行)
这一切都是为了开始

如果要使用模块
csv
请继续


享受。

欢迎来到StackOverflow,请将主题作为了解主题内容和非主题内容的一部分,并查看一下!谢谢:)@Alexander为什么不从这里开始?打扰一下我的代码有什么问题吗?@alexzander不,很抱歉,这是你对我评论的回答。OP(从今天起成为会员)完全没有表现出他们的努力。这就是为什么我要指出规则。