-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscreenshot2code.py
More file actions
213 lines (187 loc) · 6.84 KB
/
screenshot2code.py
File metadata and controls
213 lines (187 loc) · 6.84 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import shutil
import sys
from typing import Tuple, Union
import pandas as pd
import pytesseract as tess
from guesslang import Guess
from PIL import Image
from pytesseract import Output
log_file = open("log", "w")
class Screenshot2Code:
@staticmethod
def version_check():
print("Python version: ", sys.version)
if sys.version_info < (3, 9):
raise Exception("This program requires at least Python 3.9")
@staticmethod
def copy_to_clipboard(filename: Union[str, None]):
# maybe something went wrong?
if filename is None:
raise Exception(f"filename `{filename}` argument is empty")
if not os.path.isfile(filename):
raise Exception(f"`{filename}` is not a file")
print("Copying to clipboard...")
# TODO: use filename instead of text value for Win32 and MacOS
if sys.platform == "win32":
# Windows
command = "echo " + filename.strip() + "| clip"
os.system(command)
elif sys.platform == "darwin":
# macOS
command = "echo " + filename.strip() + "| pbcopy"
os.system(command)
else:
# Linux and other Unix-based systems
if not shutil.which("xsel"):
raise Exception("You do not have `xsel` installed.")
command = "xsel --clipboard <" + filename
os.system(command)
# Set the path to the Tesseract OCR executable
@staticmethod
def check_for_tesseract():
tess_cmd = shutil.which("tesseract")
if tess_cmd is None:
raise Exception("Please make sure you have tesseract installed")
tess.pytesseract.tesseract_cmd = tess_cmd
return True
# FIXME: enforce dealing with the TESSDATA_PREFIX prefix
@staticmethod
def check_for_tessdata_prefix() -> bool:
if os.environ.get("TESSDATA_PREFIX"):
print("log: TESSDATA_PREFIX has been defined", file=sys.stderr)
else:
# not sure how to deal with this yet
os.environ["TESSDATA_PREFIX"] = "./tess_data_bak"
return True
# FIXME: sometime the space formatting is very wrong
@staticmethod
def preserve_identation(frame: pd.DataFrame) -> str:
df1 = frame[(frame.conf != "-1") & (frame.text != " ") & (frame.text != "")]
# sort blocks vertically
code = ""
sorted_blocks = (
df1.groupby("block_num").first().sort_values("top").index.tolist()
)
for block in sorted_blocks:
curr = df1[df1["block_num"] == block]
sel = curr[curr.text.str.len() > 3]
char_w = (sel.width / sel.text.str.len()).mean()
prev_par, prev_line, prev_left = 0, 0, 0
text = ""
for ix, ln in curr.iterrows():
# add new line when necessary
if prev_par != ln["par_num"]:
text += "\n"
prev_par = ln["par_num"]
prev_line = ln["line_num"]
prev_left = 0
elif prev_line != ln["line_num"]:
text += "\n"
prev_line = ln["line_num"]
prev_left = 0
added = 0 # num of spaces that should be added
if ln["left"] / char_w > prev_left + 1:
added = int((ln["left"]) / char_w) - prev_left
text += " " * 2 * added # go extra on identation by default
text += ln["text"] + " "
prev_left += len(ln["text"]) + added + 1
text += "\n"
code += text
return code
@staticmethod
def guess_lang(text_in: str) -> Union[str, None]:
print(text_in, file=log_file)
guess = Guess()
name = guess.language_name(text_in)
print(f"The language guessed is {name}", file=log_file)
return name
@staticmethod
def lang_to_extension(lang: str) -> Union[str, None]:
lang_extensions = {
"Assembly": ".asm",
"Batchfile": ".bat",
"C": ".c",
"C#": ".cs",
"C++": ".cpp",
"Clojure": ".clj",
"CMake": ".cmake",
"COBOL": ".cbl",
"CoffeeScript": ".coffee",
"CSS": ".css",
"CSV": ".csv",
"Dart": ".dart",
"DM": ".dm",
"Dockerfile": ".dockerfile",
"Elixir": ".ex",
"Erlang": ".erl",
"Fortran": ".f",
"Go": ".go",
"Groovy": ".groovy",
"Haskell": ".hs",
"HTML": ".html",
"INI": ".ini",
"Java": ".java",
"JavaScript": ".js",
"JSON": ".json",
"Julia": ".jl",
"Kotlin": ".kt",
"Lisp": ".lisp",
"Lua": ".lua",
"Makefile": ".mk",
"Markdown": ".md",
"Matlab": ".m",
"Objective-C": ".m",
"OCaml": ".ml",
"Pascal": ".pas",
"Perl": ".pl",
"PHP": ".php",
"PowerShell": ".ps1",
"Prolog": ".pl",
"Python": ".py",
"R": ".R",
"Ruby": ".rb",
"Rust": ".rs",
"Scala": ".scala",
"Shell": ".sh",
"SQL": ".sql",
"Swift": ".swift",
"TeX": ".tex",
"TOML": ".toml",
"TypeScript": ".ts",
"Verilog": ".v",
"Visual Basic": ".vb",
"XML": ".xml",
"YAML": ".yml",
}
return lang_extensions.get(lang, None)
def convert(self, image_path: str) -> Tuple[Union[str, None], Union[str, None]]:
try:
img = Image.open(image_path)
# Custom Tesseract configuration for preserving whitespace and formatting
config = r"-c preserve_interword_spaces=1 --psm 6 --oem 3"
text_data = tess.image_to_data(img, config=config, output_type=Output.DICT)
frame = pd.DataFrame(text_data)
text = self.preserve_identation(frame)
lang = self.guess_lang(text)
return lang, text
except Exception as e:
print("Error:", str(e), file=log_file)
return None, None
if __name__ == "__main__":
S2C = Screenshot2Code()
S2C.version_check()
if S2C.check_for_tesseract() is False:
print("Please make sure you have tesseract installed.", file=sys.stderr)
exit(1)
S2C.check_for_tessdata_prefix()
if len(sys.argv) == 3:
image_path = sys.argv[1]
output_path = sys.argv[2]
lang, text = S2C.convert(image_path)
with open(output_path, "w") as f:
if text:
f.write(text)
S2C.copy_to_clipboard(output_path)
else:
print("Usage: python screenshot2code.py <screenshot_path> <output_path>")