3
3
from PIL import ImageGrab
4
4
from PIL import Image
5
5
import os
6
+ from pathlib import Path
6
7
import sys
7
- from typing import Tuple
8
+ from typing import List , Optional , Tuple
8
9
import atexit
9
10
from contextlib import suppress
10
11
import logging
@@ -105,9 +106,9 @@ def __call__(self, img=None, resize=True) -> str:
105
106
img = None
106
107
if img is None :
107
108
if self .last_pic is None :
108
- print ('Provide an image.' )
109
109
return ''
110
110
else :
111
+ print ('\n Last image is: ' , end = '' )
111
112
img = self .last_pic .copy ()
112
113
else :
113
114
self .last_pic = img .copy ()
@@ -177,6 +178,36 @@ def output_prediction(pred, args):
177
178
webbrowser .open (url )
178
179
179
180
181
+ def predict (model , file , arguments ):
182
+ img = None
183
+ if file :
184
+ try :
185
+ img = Image .open (os .path .expanduser (file ))
186
+ except Exception as e :
187
+ print (e , end = '' )
188
+ else :
189
+ try :
190
+ img = ImageGrab .grabclipboard ()
191
+ except NotImplementedError as e :
192
+ print (e , end = '' )
193
+ pred = model (img )
194
+ output_prediction (pred , arguments )
195
+
196
+ def check_file_path (paths :List [Path ], wdir :Optional [Path ]= None )-> List [str ]:
197
+ files = []
198
+ for path in paths :
199
+ if type (path )== str :
200
+ if path == '' :
201
+ continue
202
+ path = Path (path )
203
+ pathsi = ([path ] if wdir is None else [path , wdir / path ])
204
+ for p in pathsi :
205
+ if p .exists ():
206
+ files .append (str (p .resolve ()))
207
+ elif '*' in path .name :
208
+ files .extend ([str (pi .resolve ()) for pi in p .parent .glob (p .name )])
209
+ return list (set (files ))
210
+
180
211
def main (arguments ):
181
212
path = user_data_dir ('pix2tex' )
182
213
os .makedirs (path , exist_ok = True )
@@ -187,20 +218,31 @@ def main(arguments):
187
218
with suppress (OSError ):
188
219
readline .read_history_file (history_file )
189
220
atexit .register (readline .write_history_file , history_file )
221
+ files = check_file_path (arguments .file )
222
+ wdir = Path (os .getcwd ())
190
223
with in_model_path ():
191
224
model = LatexOCR (arguments )
192
- file = None
225
+ if files :
226
+ for file in check_file_path (arguments .file , wdir ):
227
+ print (file + ': ' , end = '' )
228
+ predict (model , file , arguments )
229
+ model .last_pic = None
230
+ with suppress (NameError ):
231
+ readline .add_history (file )
232
+ exit ()
233
+ pat = re .compile (r't=([\.\d]+)' )
193
234
while True :
194
235
try :
195
- instructions = input ('Predict LaTeX code for image ("?"/" h" for help). ' )
236
+ instructions = input ('Predict LaTeX code for image ("h" for help). ' )
196
237
except KeyboardInterrupt :
197
238
# TODO: make the last line gray
198
239
print ("" )
199
240
continue
200
241
except EOFError :
201
242
break
202
- possible_file = instructions .strip ()
203
- ins = possible_file .lower ()
243
+ file = instructions .strip ()
244
+ ins = file .lower ()
245
+ t = pat .match (ins )
204
246
if ins == 'x' :
205
247
break
206
248
elif ins in ['?' , 'h' , 'help' ]:
@@ -231,26 +273,18 @@ def main(arguments):
231
273
setattr (arguments , ins , not getattr (arguments , ins , False ))
232
274
print ('set %s to %s' % (ins , getattr (arguments , ins )))
233
275
continue
234
- elif os .path .isfile (os .path .realpath (possible_file )):
235
- file = possible_file
236
- else :
237
- t = re .match (r't=([\.\d]+)' , ins )
238
- if t is not None :
239
- t = t .groups ()[0 ]
240
- model .args .temperature = float (t )+ 1e-8
241
- print ('new temperature: T=%.3f' % model .args .temperature )
242
- continue
243
- try :
244
- img = None
245
- if file :
246
- img = Image .open (file )
276
+ elif t is not None :
277
+ t = t .groups ()[0 ]
278
+ model .args .temperature = float (t )+ 1e-8
279
+ print ('new temperature: T=%.3f' % model .args .temperature )
280
+ continue
281
+ files = check_file_path (file .split (' ' ), wdir )
282
+ with suppress (KeyboardInterrupt ):
283
+ if files :
284
+ for file in files :
285
+ if len (files )> 1 :
286
+ print (file + ': ' , end = '' )
287
+ predict (model , file , arguments )
247
288
else :
248
- try :
249
- img = ImageGrab .grabclipboard ()
250
- except :
251
- pass
252
- pred = model (img )
253
- output_prediction (pred , arguments )
254
- except KeyboardInterrupt :
255
- pass
289
+ predict (model , file , arguments )
256
290
file = None
0 commit comments