Skip to content

Commit f85a9d7

Browse files
committed
Support slurp & grim as screenshot tools
gnome-screenshot doesn't work with wlroots-based Wayland compositors, use grim if available.
1 parent f01059a commit f85a9d7

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

pix2tex/gui.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from shutil import which
2+
import io
3+
import subprocess
24
import sys
35
import os
46
import tempfile
@@ -54,11 +56,11 @@ def initUI(self):
5456

5557
# Create snip button
5658
if sys.platform == "darwin":
57-
self.snipButton = QPushButton('Snip [Option+S]', self)
58-
self.snipButton.clicked.connect(self.onClick)
59+
self.snipButton = QPushButton('Snip [Option+S]', self)
60+
self.snipButton.clicked.connect(self.onClick)
5961
else:
60-
self.snipButton = QPushButton('Snip [Alt+S]', self)
61-
self.snipButton.clicked.connect(self.onClick)
62+
self.snipButton = QPushButton('Snip [Alt+S]', self)
63+
self.snipButton.clicked.connect(self.onClick)
6264

6365
self.shortcut = QtGui.QShortcut(QtGui.QKeySequence('Alt+S'), self)
6466
self.shortcut.activated.connect(self.onClick)
@@ -95,21 +97,23 @@ def toggleProcessing(self, value=None):
9597
else:
9698
if sys.platform == "darwin":
9799
text = 'Snip [Option+S]'
98-
else:
100+
else:
99101
text = 'Snip [Alt+S]'
100102
func = self.onClick
101103
self.retryButton.setEnabled(True)
102104
self.shortcut.setEnabled(not self.isProcessing)
103105
self.snipButton.setText(text)
104106
self.snipButton.clicked.disconnect()
105107
self.snipButton.clicked.connect(func)
106-
self.displayPrediction()
108+
self.displayPrediction()
107109

108110
@pyqtSlot()
109111
def onClick(self):
110112
self.close()
111113
if which('gnome-screenshot'):
112114
self.snip_using_gnome_screenshot()
115+
elif which('grim') and which('slurp'):
116+
self.snip_using_grim()
113117
else:
114118
self.snipWidget.snip()
115119

@@ -123,14 +127,31 @@ def interrupt(self):
123127
def snip_using_gnome_screenshot(self):
124128
try:
125129
with tempfile.NamedTemporaryFile() as tmp:
126-
os.system(f"gnome-screenshot --area --file={tmp.name}")
130+
subprocess.run(["gnome-screenshot", "--area", f"--file={tmp.name}"])
127131
# Use `tmp.name` instead of `tmp.file` due to compatability issues between Pillow and tempfile
128132
self.returnSnip(Image.open(tmp.name))
129133
except:
130134
print(f"Failed to load saved screenshot! Did you cancel the screenshot?")
131135
print("If you don't have gnome-screenshot installed, please install it.")
132136
self.returnSnip()
133137

138+
def snip_using_grim(self):
139+
try:
140+
p = subprocess.run('slurp',
141+
check=True,
142+
capture_output=True,
143+
text=True)
144+
geometry = p.stdout.strip()
145+
146+
p = subprocess.run(['grim', '-g', geometry, '-'],
147+
check=True,
148+
capture_output=True)
149+
self.returnSnip(Image.open(io.BytesIO(p.stdout)))
150+
except:
151+
print(f"Failed to load saved screenshot! Did you cancel the screenshot?")
152+
print("If you don't have slurp and grim installed, please install them.")
153+
self.returnSnip()
154+
134155
def returnSnip(self, img=None):
135156
self.toggleProcessing(True)
136157
self.retryButton.setEnabled(False)

0 commit comments

Comments
 (0)