1. 程式人生 > >識別圖片內容,並將相應內容寫到對應文字檔案中

識別圖片內容,並將相應內容寫到對應文字檔案中

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 18 17:05:47 2019

@author: HeyJude
"""
import time
start_time = time.time()

def GetText(pic_path, text_path):
    import pytesseract
    from PIL import Image
    import os
    files = os.listdir(pic_path)
    for file in files:
        file_name = pic_path + file
        pytesseract.pytesseract.tesseract_cmd = "D:/Program Files/Tesseract-OCR/tesseract.exe"
        text = pytesseract.image_to_string(Image.open(file_name))
        f = open(text_path + file.split(".")[0] + '.txt', "w")
        print(text)
        print("*************************")
        f.write(str(text))
        f.close()
    print("Mission Completed!")

pic_path = "D:/data/test_pic_20190328/"
text_path = "D:/data/test_pic_info_0328/"


if __name__ == "__main__":
    GetText(pic_path, text_path)
    end_time = time.time()
    print("\nRunning time: %f s" % (end_time - st