-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix --file
and support expanding ~
#187
Conversation
Have rebased to solve conflict. ❯ pix2tex -f ~/Desktop/a.png
S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\}, |
$ pix2tex *.png
a.png: \alpha
b.png: \beta
... |
Yes, that would be useful! |
I have tested it can work for me. Please test in your machine 😄 ❯ pix2tex
Predict LaTeX code for image ("?"/"h" for help). /
[Errno 21] Is a directory: '/'
Predict LaTeX code for image ("?"/"h" for help). /home/wzy/Desktop/a.png
S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help). empty
[Errno 2] No such file or directory: 'empty'
Last image is: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help).
ImageGrab.grabclipboard() is macOS and Windows only
Last image is: S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help). %
❯ pix2tex / ~/Desktop/a.png empty ~/Desktop/a.png
/: [Errno 21] Is a directory: '/'
/home/wzy/Desktop/a.png: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
empty: [Errno 2] No such file or directory: 'empty'
/home/wzy/Desktop/a.png: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\}, |
In
Also the glob doesn't work for me. |
Fixed. |
Here is glob and relative path support. Can you apply the patch please? # pix2tex/cli.py
@@ -3,8 +3,9 @@ import pandas.io.clipboard as clipboard
from PIL import ImageGrab
from PIL import Image
import os
+from pathlib import Path
import sys
-from typing import Tuple
+from typing import List, Optional, Tuple
import atexit
from contextlib import suppress
import logging
@@ -178,21 +179,34 @@ def output_prediction(pred, args):
def predict(model, file, arguments):
- with suppress(KeyboardInterrupt):
- img = None
- if file:
- try:
- img = Image.open(os.path.expanduser(file))
- except Exception as e:
- print(e, end='')
- else:
- try:
- img = ImageGrab.grabclipboard()
- except NotImplementedError as e:
- print(e, end='')
- pred = model(img)
- output_prediction(pred, arguments)
+ img = None
+ if file:
+ try:
+ img = Image.open(os.path.expanduser(file))
+ except Exception as e:
+ print(e, end='')
+ else:
+ try:
+ img = ImageGrab.grabclipboard()
+ except NotImplementedError as e:
+ print(e, end='')
+ pred = model(img)
+ output_prediction(pred, arguments)
+def check_file_path(paths:List[Path], wdir:Optional[Path]=None)->List[str]:
+ files = []
+ for path in paths:
+ if type(path)==str:
+ if path=='':
+ continue
+ path=Path(path)
+ pathsi = ([path] if wdir is None else [path, wdir/path])
+ for p in pathsi:
+ if p.exists():
+ files.append(str(p.resolve()))
+ elif '*' in path.name:
+ files.extend([str(pi.resolve()) for pi in p.parent.glob(p.name)])
+ return list(set(files))
def main(arguments):
path = user_data_dir('pix2tex')
@@ -204,11 +218,12 @@ def main(arguments):
with suppress(OSError):
readline.read_history_file(history_file)
atexit.register(readline.write_history_file, history_file)
- files = list(map(lambda file: os.path.realpath(file), arguments.file))
+ files = check_file_path(arguments.file)
+ wdir = Path(os.getcwd())
with in_model_path():
model = LatexOCR(arguments)
if files:
- for file in files:
+ for file in check_file_path(arguments.file, wdir):
print(file + ': ', end='')
predict(model, file, arguments)
model.last_pic = None
@@ -218,7 +233,7 @@ def main(arguments):
pat = re.compile(r't=([\.\d]+)')
while True:
try:
- instructions = input('Predict LaTeX code for image ("?"/"h" for help). ')
+ instructions = input('Predict LaTeX code for image ("h" for help). ')
except KeyboardInterrupt:
# TODO: make the last line gray
print("")
@@ -263,5 +278,13 @@ def main(arguments):
model.args.temperature = float(t)+1e-8
print('new temperature: T=%.3f' % model.args.temperature)
continue
- predict(model, file, arguments)
+ files = check_file_path(file.split(' '), wdir)
+ with suppress(KeyboardInterrupt):
+ if files:
+ for file in files:
+ if len(files)>1:
+ print(file + ': ', end='')
+ predict(model, file, arguments)
+ else:
+ predict(model, file, arguments)
file = None |
I know I can but I'm not allowed to because of my contract rn. diff --git a/pix2tex/cli.py b/pix2tex/cli.py
index f590896..57867f6 100644
--- a/pix2tex/cli.py
+++ b/pix2tex/cli.py
@@ -3,8 +3,9 @@ import pandas.io.clipboard as clipboard
from PIL import ImageGrab
from PIL import Image
import os
+from pathlib import Path
import sys
-from typing import Tuple
+from typing import List, Optional, Tuple
import atexit
from contextlib import suppress
import logging
@@ -178,21 +179,34 @@ def output_prediction(pred, args):
def predict(model, file, arguments):
- with suppress(KeyboardInterrupt):
- img = None
- if file:
- try:
- img = Image.open(os.path.expanduser(file))
- except Exception as e:
- print(e, end='')
- else:
- try:
- img = ImageGrab.grabclipboard()
- except NotImplementedError as e:
- print(e, end='')
- pred = model(img)
- output_prediction(pred, arguments)
+ img = None
+ if file:
+ try:
+ img = Image.open(os.path.expanduser(file))
+ except Exception as e:
+ print(e, end='')
+ else:
+ try:
+ img = ImageGrab.grabclipboard()
+ except NotImplementedError as e:
+ print(e, end='')
+ pred = model(img)
+ output_prediction(pred, arguments)
+def check_file_path(paths:List[Path], wdir:Optional[Path]=None)->List[str]:
+ files = []
+ for path in paths:
+ if type(path)==str:
+ if path=='':
+ continue
+ path=Path(path)
+ pathsi = ([path] if wdir is None else [path, wdir/path])
+ for p in pathsi:
+ if p.exists():
+ files.append(str(p.resolve()))
+ elif '*' in path.name:
+ files.extend([str(pi.resolve()) for pi in p.parent.glob(p.name)])
+ return list(set(files))
def main(arguments):
path = user_data_dir('pix2tex')
@@ -204,11 +218,12 @@ def main(arguments):
with suppress(OSError):
readline.read_history_file(history_file)
atexit.register(readline.write_history_file, history_file)
- files = list(map(lambda file: os.path.realpath(file), arguments.file))
+ files = check_file_path(arguments.file)
+ wdir = Path(os.getcwd())
with in_model_path():
model = LatexOCR(arguments)
if files:
- for file in files:
+ for file in check_file_path(arguments.file, wdir):
print(file + ': ', end='')
predict(model, file, arguments)
model.last_pic = None
@@ -218,7 +233,7 @@ def main(arguments):
pat = re.compile(r't=([\.\d]+)')
while True:
try:
- instructions = input('Predict LaTeX code for image ("?"/"h" for help). ')
+ instructions = input('Predict LaTeX code for image ("h" for help). ')
except KeyboardInterrupt:
# TODO: make the last line gray
print("")
@@ -263,5 +278,13 @@ def main(arguments):
model.args.temperature = float(t)+1e-8
print('new temperature: T=%.3f' % model.args.temperature)
continue
- predict(model, file, arguments)
+ files = check_file_path(file.split(' '), wdir)
+ with suppress(KeyboardInterrupt):
+ if files:
+ for file in files:
+ if len(files)>1:
+ print(file + ': ', end='')
+ predict(model, file, arguments)
+ else:
+ predict(model, file, arguments)
file = None |
OK. |
Fix
--file
and support expanding~
Fix #176