-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageToText.py
More file actions
31 lines (22 loc) · 902 Bytes
/
Copy pathimageToText.py
File metadata and controls
31 lines (22 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
from PIL import Image
from pytesseract import *
try:
from config import TESSERACT_PATH
except ImportError:
TESSERACT_PATH = None
if TESSERACT_PATH:
pytesseract.tesseract_cmd = TESSERACT_PATH
images_folder = "Images"
text_folder = "Text"
os.makedirs(text_folder, exist_ok = True)
for img_name in os.listdir(images_folder):
if img_name.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".gif")):
img_path = os.path.join(images_folder, img_name)
img = Image.open(img_path)
output = pytesseract.image_to_string(img)
text_file_name = os.path.splitext(img_name)[0] + ".txt"
text_file_path = os.path.join(text_folder, text_file_name)
with open(text_file_path, "w", encoding = "utf-8") as text_file:
text_file.write(output)
print(f"Processed {img_name} to {text_file_name}")