-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
61 lines (48 loc) · 1.63 KB
/
main.py
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
59
60
61
import sys
import os
from subprocess import call
VALID_IMAGES = [".jpg",".gif",".png",".tga",".tif",".bmp"]
FNULL = open(os.devnull, 'w')
class ArgumentMissingException(Exception):
def __init__(self):
print("usage: {} <dirname>".format(sys.argv[0]))
sys.exit(1)
def create_directory(path):
if not os.path.exists(path):
os.makedirs(path)
def check_path(path):
return bool(os.path.exists(path))
def main(path):
if call(['which', 'tesseract']):
print("tesseract-ocr missing, use sudo apt-get install tesseract-ocr to resolve")
elif check_path(path):
directory_path = path + '/converted-text/'
count = 0
other_files = 0
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() not in VALID_IMAGES:
other_files += 1
continue
else :
if count == 0:
create_directory(directory_path)
count += 1
image_file_name = path + '/' + f
filename = os.path.splitext(f)[0]
filename = ''.join(e for e in filename if e.isalnum() or e == '-')
text_file_path = directory_path + filename
call(["tesseract", image_file_name, text_file_path], stdout=FNULL)
print(str(count) + (" file" if count == 1 else " files") + " completed")
if count + other_files == 0:
print("No files found at your given location")
else :
print(str(count) + " / " + str(count + other_files) + " files converted")
else :
print("No directory found at " + format(path))
if __name__ == '__main__':
if len(sys.argv) != 2:
raise ArgumentMissingException
path = sys.argv[1]
path = os.path.abspath(path)
main("C:\Users\chira\OneDrive\Pictures\pic")