📜  Python|对同时存在于文件夹中的所有图像进行 OCR

📅  最后修改于: 2022-05-13 01:54:24.573000             🧑  作者: Mango

Python|对同时存在于文件夹中的所有图像进行 OCR

如果您有一个充满图像的文件夹,其中包含一些需要提取到具有相应图像文件名或单个文件的单独文件夹中的文本,那么这就是您正在寻找的完美代码。

本文不仅为您提供OCR(光学字符识别)的基础,还帮助您为主文件夹中的每个图像创建output.txt文件并将其保存在某个预定方向。

需要的图书馆——

pip3 install pillow
pip3 install os-sys

您还需要tesseract-octpytesseract库。 tesseract-ocr可以从这里下载和安装, pytesseract可以使用pip3 install pytesseract

下面是Python的实现——
# Python program to extract text from all the images in a folder
# storing the text in corresponding files in a different folder
from PIL import Image
import pytesseract as pt
import os
      
def main():
    # path for the folder for getting the raw images
    path ="E:\\GeeksforGeeks\\images"
  
    # path for the folder for getting the output
    tempPath ="E:\\GeeksforGeeks\\textFiles"
  
    # iterating the images inside the folder
    for imageName in os.listdir(path):
              
        inputPath = os.path.join(path, imageName)
        img = Image.open(inputPath)
  
        # applying ocr using pytesseract for python
        text = pt.image_to_string(img, lang ="eng")
  
        # for removing the .jpg from the imagePath
        imagePath = imagePath[0:-4]
  
        fullTempPath = os.path.join(tempPath, 'time_'+imageName+".txt")
        print(text)
  
        # saving the  text for every image in a separate .txt file
        file1 = open(fullTempPath, "w")
        file1.write(text)
        file1.close() 
  
if __name__ == '__main__':
    main()

输入图像:

image_sample1

输出 :

geeksforgeeks
geeksforgeeks

如果您想将图像中的所有文本存储在一个输出文件中,那么代码会有所不同。主要区别在于,我们将要写入的文件的模式将更改为“ +a ”以附加文本并创建output.txt文件(如果它尚不存在)。

# extract text from all the images in a folder
# storing the text in a single file
from PIL import Image
import pytesseract as pt
import os
      
def main():
    # path for the folder for getting the raw images
    path ="E:\\GeeksforGeeks\\images"
  
    # link to the file in which output needs to be kept
    fullTempPath ="E:\\GeeksforGeeks\\output\\outputFile.txt"
  
    # iterating the images inside the folder
    for imageName in os.listdir(path):
        inputPath = os.path.join(path, imageName)
        img = Image.open(inputPath)
  
        # applying ocr using pytesseract for python
        text = pt.image_to_string(img, lang ="eng")
  
        # saving the  text for appending it to the output.txt file
        # a + parameter used for creating the file if not present
        # and if present then append the text content
        file1 = open(fullTempPath, "a+")
  
        # providing the name of the image
        file1.write(imageName+"\n")
  
        # providing the content in the image
        file1.write(text+"\n")
        file1.close() 
  
    # for printing the output file
    file2 = open(fullTempPath, 'r')
    print(file2.read())
    file2.close()        
  
  
if __name__ == '__main__':
    main()

输入图像:

image_sample1


image_sample2

输出:

它给出了从文件夹内的图像中提取所有信息后创建的单个文件的输出。文件的格式是这样的——

Name of the image
Content of the image
Name of the next image and so on .....