1
1
from shutil import which
2
+ import io
3
+ import subprocess
2
4
import sys
3
5
import os
4
6
import tempfile
@@ -54,11 +56,11 @@ def initUI(self):
54
56
55
57
# Create snip button
56
58
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 )
59
61
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 )
62
64
63
65
self .shortcut = QtGui .QShortcut (QtGui .QKeySequence ('Alt+S' ), self )
64
66
self .shortcut .activated .connect (self .onClick )
@@ -95,21 +97,23 @@ def toggleProcessing(self, value=None):
95
97
else :
96
98
if sys .platform == "darwin" :
97
99
text = 'Snip [Option+S]'
98
- else :
100
+ else :
99
101
text = 'Snip [Alt+S]'
100
102
func = self .onClick
101
103
self .retryButton .setEnabled (True )
102
104
self .shortcut .setEnabled (not self .isProcessing )
103
105
self .snipButton .setText (text )
104
106
self .snipButton .clicked .disconnect ()
105
107
self .snipButton .clicked .connect (func )
106
- self .displayPrediction ()
108
+ self .displayPrediction ()
107
109
108
110
@pyqtSlot ()
109
111
def onClick (self ):
110
112
self .close ()
111
113
if which ('gnome-screenshot' ):
112
114
self .snip_using_gnome_screenshot ()
115
+ elif which ('grim' ) and which ('slurp' ):
116
+ self .snip_using_grim ()
113
117
else :
114
118
self .snipWidget .snip ()
115
119
@@ -123,14 +127,31 @@ def interrupt(self):
123
127
def snip_using_gnome_screenshot (self ):
124
128
try :
125
129
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 } "] )
127
131
# Use `tmp.name` instead of `tmp.file` due to compatability issues between Pillow and tempfile
128
132
self .returnSnip (Image .open (tmp .name ))
129
133
except :
130
134
print (f"Failed to load saved screenshot! Did you cancel the screenshot?" )
131
135
print ("If you don't have gnome-screenshot installed, please install it." )
132
136
self .returnSnip ()
133
137
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
+
134
155
def returnSnip (self , img = None ):
135
156
self .toggleProcessing (True )
136
157
self .retryButton .setEnabled (False )
0 commit comments