I am doing OCR on image.
from PIL import Image
import pytesseract
from pytesseract import image_to_string
img1=Image.open('my.png')
print(image_to_string(img1))
How can save the extracted information into text file called "Output.txt"
You could create a unique folder name using uuid
, and then write the output.txt to it like so?:
from uuid import uuid4
import os
folder_name = str(uuid4())
os.makedirs(folder_name)
with open('./{fn}/output.txt'.format(fn=folder_name),'wb') as f:
f.write(image_to_string(img1))
I found the easy way to save OCR output into text file
def ocr(file_to_ocr):
im = Image.open(file_to_ocr)
txt=pytesseract.image_to_string(im)
return txt
directory = os.path.join("Your_path")
for root,dirs,files in os.walk(directory):
for file in files:
if file.endswith(".jpg"):
pre_fix=file[:-4]
txt=ocr(file)
with open(directory+"\\"+pre_fix+".txt",'w') as f: f.write(str(txt))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.