-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyextract
executable file
·85 lines (72 loc) · 2.71 KB
/
pyextract
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
import pytesseract
from PIL import ImageGrab, Image
import pyperclip
import yaml
from pathlib import Path
import os
def load_config():
executable_path = os.path.abspath(__file__)
executable_dir = os.path.dirname(executable_path)
with open(f"{executable_dir}/config.yaml", "r") as file:
yaml_data = yaml.safe_load(file)
return yaml_data
def get_image_from_clipboard():
try:
image = ImageGrab.grabclipboard()
if isinstance(image, Image.Image):
return image
else:
print("No image in clipboard")
return None
except Exception as e:
print("Error:", e)
return None
def is_image_file(filename):
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff']
return any(filename.suffix.lower() == ext for ext in image_extensions)
def image_to_string(image, language):
return pytesseract.image_to_string(image, lang=language)
def main():
yaml_data = load_config()
result = []
if yaml_data["use_clipboard_for_input"]:
image = get_image_from_clipboard()
if image is None:
return
try:
result = image_to_string(image, yaml_data["language"])
except pytesseract.pytesseract.TesseractError as e:
print(f"Pytesseract encountered an error: {e}")
else:
directory = Path(os.path.expanduser(yaml_data["path_to_directory"]))
files = [entry for entry in directory.iterdir() if entry.is_file()]
for file in files:
if is_image_file(file):
with Image.open(file) as img:
img_to_append = image_to_string(img, yaml_data["language"])
result.append([file, img_to_append])
if yaml_data["save_data"]:
save_directory = Path(os.path.expanduser(yaml_data["save_location"]))
save_directory.mkdir(parents=True, exist_ok=True)
if isinstance(result, str):
file_count = len(list(save_directory.glob('*.txt')))
with open(save_directory / f"{file_count+1}.txt", "w") as file:
file.write(result)
else:
for item in result:
with open(item[0].with_suffix('.txt'), "w") as file:
file.write(item[1])
else:
if not yaml_data["use_clipboard_for_input"]:
print("use_clipboard_for_input can't be false when save_data is false")
return
pyperclip.copy(result)
if yaml_data["print_result"]:
if isinstance(result, str):
print(result)
else:
for item in result:
print(f"{item[0].name}\n{item[1]}\n")
if __name__ == "__main__":
main()