-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathtext_extract.py
More file actions
58 lines (42 loc) · 1.65 KB
/
text_extract.py
File metadata and controls
58 lines (42 loc) · 1.65 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from PIL import Image
import pytesseract as pt
import os
from pathlib import Path
current_location = os.getcwd() + "\\"
def extract():
"""
Function for extracting text from images.
Additional it saves the text extracted as a txt file.
"""
# Enter the name of folder which contains img files
image_location = input("Enter the Folder name containing Images: ")
image_path = os.path.join(current_location, image_location)
# Enter the name of folder which would contain respective txt files
destination = input("Enter your desired output location: ")
destination_path = os.path.join(current_location, destination)
# Path to Tesseract
tesseract_path = input("Enter the Path to Tesseract: ")
print(
"\nNOTE: "
"It is preferable to setup the PATH variable to Tesseract, see README. \n"
)
# = r'C:\Program Files\Tesseract-OCR\tesseract'
pt.pytesseract.tesseract_cmd = tesseract_path
# iterating over the images in the folder
for imageName in os.listdir(image_path):
# Join the path and image name to obtain absolute path
inputPath = os.path.join(image_path, imageName)
img = Image.open(inputPath)
# OCR
text = pt.image_to_string(img, lang="eng")
# Removing extensions
img_file = Path(inputPath).stem
print(img_file)
# The output text file
text_file = img_file + ".txt"
output_path = os.path.join(destination_path, text_file)
# saving the text for every image in a separate .txt file
with open(output_path, "w") as file:
file.write(text)
if __name__ == "__main__":
extract()