-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot-grim.py
More file actions
executable file
·349 lines (317 loc) · 10.3 KB
/
screenshot-grim.py
File metadata and controls
executable file
·349 lines (317 loc) · 10.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/python3
# SPDX-License-Identifier: ISC
# GIMP plug-in for taking screenshots of Wayland desktops
#
# Copyright (C) 2025 Andreas Vögele <andreas@andreasvoegele.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import gi
gi.require_version("Gimp", "3.0")
from gi.repository import Gimp
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
gi.require_version("GimpUi", "3.0")
from gi.repository import GimpUi
import errno, subprocess, sys, tempfile, time
# Localize the module
import gettext, os
plugindir = os.path.dirname(__file__)
localedir = os.path.join(plugindir, "locale")
if not os.path.isdir(localedir):
localedir = None
try:
t = gettext.translation("gimp-screenshot-grim-plugin", localedir)
_ = t.gettext
except:
# fmt: off
def _(message): return message
def slurp():
args = ["slurp", "-d"]
proc = subprocess.run(args, capture_output=True, text=True)
if proc.returncode != 0:
errmsg = proc.stderr
if not "selection cancelled" in errmsg:
raise OSError(errno.EINVAL, errmsg)
return proc.stdout.rstrip()
def grim(
file,
include_pointer=False,
level=None,
output=None,
quality=None,
region=None,
scale_factor=None,
type=None,
):
args = ["grim"]
if include_pointer:
args.append("-c")
if level is not None:
args.append("-l")
args.append(str(level))
if output is not None and output != "":
args.append("-o")
args.append(output)
if quality is not None:
args.append("-q")
args.append(str(quality))
if region is not None and region != "":
args.append("-g")
args.append(region)
if scale_factor is not None:
args.append("-s")
args.append(str(scale_factor))
if type is not None:
args.append("-t")
args.append(type)
args.append("-")
proc = subprocess.Popen(args, stdout=file, stderr=subprocess.PIPE)
try:
_, errs = proc.communicate(timeout=15)
except subprocess.TimeoutExpired:
proc.kill()
_, errs = proc.communicate()
if proc.returncode != 0:
errmsg = str(errs, errors="ignore")
raise OSError(errno.EINVAL, errmsg)
def shoot_dialog(procedure, config):
names = [_.name for _ in procedure.get_arguments() if _.name != "run-mode"]
dialog = GimpUi.ProcedureDialog(
procedure=procedure, config=config, title=_("Screenshot")
)
dialog.set_ok_label(_("S_nap"))
dialog.fill(names)
ok = dialog.run()
dialog.destroy()
return ok
def screenshot_run(procedure, config, data):
run_mode = config.get_property("run-mode")
output = ""
region = ""
if run_mode == Gimp.RunMode.INTERACTIVE:
GimpUi.init("plug-in-screenshot-grim")
if not shoot_dialog(procedure, config):
return procedure.new_return_values(
Gimp.PDBStatusType.CANCEL,
GLib.Error(),
)
shoot_type = config.get_property("shoot-type")
include_pointer = config.get_property("include-pointer")
screenshot_delay = config.get_property("screenshot-delay")
if screenshot_delay > 0:
time.sleep(screenshot_delay)
if shoot_type == "region":
try:
region = slurp()
if region == "":
return procedure.new_return_values(
Gimp.PDBStatusType.CANCEL,
GLib.Error(),
)
except Exception as e:
return procedure.new_return_values(
Gimp.PDBStatusType.EXECUTION_ERROR,
GLib.Error(str(e)),
)
elif shoot_type == "output":
output = config.get_property("output")
if output == "":
return procedure.new_return_values(
Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(_("Missing Wayland output name")),
)
elif shoot_type == "rectangle":
x1 = config.get_property("x1")
y1 = config.get_property("y1")
x2 = config.get_property("x2")
y2 = config.get_property("y2")
if x2 < x1 or y2 < y1:
return procedure.new_return_values(
Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(_("Invalid rectangle coordinates")),
)
region = f"{x1},{y1} {x2 - x1 + 1}x{y2 - y1 + 1}"
else:
return procedure.new_return_values(
Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(_("Unknown shoot type") + ": " + shoot_type),
)
with tempfile.NamedTemporaryFile(
mode="wb", delete_on_close=False, suffix=".ppm"
) as fp:
try:
grim(
fp,
include_pointer=include_pointer,
output=output,
region=region,
type="ppm",
)
except Exception as e:
return procedure.new_return_values(
Gimp.PDBStatusType.EXECUTION_ERROR,
GLib.Error(str(e)),
)
finally:
fp.close()
image = Gimp.file_load(
Gimp.RunMode.NONINTERACTIVE, Gio.File.new_for_path(fp.name)
)
if image is None:
return procedure.new_return_values(
Gimp.PDBStatusType.EXECUTION_ERROR,
GLib.Error(_("Cannot load screenshot")),
)
Gimp.Display.new(image)
return Gimp.ValueArray.new_from_values(
[
GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.SUCCESS),
GObject.Value(Gimp.Image, image),
]
)
class ScreenshotGrim(Gimp.PlugIn):
_attribution = (
"Andreas Vögele", # Authors
"Andreas Vögele", # Copyright holders
"2025",
)
## GimpPlugIn virtual methods ##
def do_query_procedures(self):
return ["plug-in-screenshot-grim"]
def do_create_procedure(self, name):
if name == "plug-in-screenshot-grim":
return self._create_procedure_screenshot_grim(name)
return None
def _create_procedure_screenshot_grim(self, name):
procedure = Gimp.Procedure.new(
self,
name,
Gimp.PDBProcType.PLUGIN,
screenshot_run,
None,
)
procedure.set_menu_label(_("Screenshot with grim..."))
procedure.set_documentation(
_("Create an image from a region of a Wayland desktop"),
_(
"This plug-in takes a screenshot of a desktop region. "
"The utilities grim and slurp are required."
),
name,
)
procedure.set_attribution(*self._attribution)
procedure.add_menu_path("<Image>/File/Create")
procedure.add_enum_argument(
"run-mode",
_("Run mode"),
_("The run mode"),
Gimp.RunMode,
Gimp.RunMode.INTERACTIVE,
GObject.ParamFlags.READWRITE,
)
shoot_type_choice = Gimp.Choice.new()
shoot_type_choice.add(
"region",
0,
_("Select a region to capture"),
_("Move the selection with space bar held or cancel with Esc"),
)
shoot_type_choice.add(
"output",
1,
_("Capture an entire Wayland output"),
_('Pass the output name in "output"'),
)
shoot_type_choice.add(
"rectangle",
2,
_("Capture a region by coordinates"),
_('Pass the coordinates in "x1", "y1", "x2" and "y2"'),
)
procedure.add_choice_argument(
"shoot-type",
_("Shoot _area"),
_("The shoot type"),
shoot_type_choice,
"region",
GObject.ParamFlags.READWRITE,
)
procedure.add_string_argument(
"output",
_("_Wayland output"),
_("The name of the Wayland output to capture"),
"",
GObject.ParamFlags.READWRITE,
)
procedure.add_int_argument(
"x1",
_("Le_ft coordinate x1"),
_("Left x-coordinate"),
GLib.MININT,
GLib.MAXINT,
0,
GObject.ParamFlags.READWRITE,
)
procedure.add_int_argument(
"y1",
_("_Top coordinate y1"),
_("Top y-coordinate"),
GLib.MININT,
GLib.MAXINT,
0,
GObject.ParamFlags.READWRITE,
)
procedure.add_int_argument(
"x2",
_("R_ight coordinate x2"),
_("Right x-coordinate"),
GLib.MININT,
GLib.MAXINT,
0,
GObject.ParamFlags.READWRITE,
)
procedure.add_int_argument(
"y2",
_("_Bottom coordinate y2"),
_("Bottom y-coordinate"),
GLib.MININT,
GLib.MAXINT,
0,
GObject.ParamFlags.READWRITE,
)
procedure.add_boolean_argument(
"include-pointer",
_("Include _mouse pointer"),
_("Your pointing device's cursor will be part of the image"),
False,
GObject.ParamFlags.READWRITE,
)
procedure.add_int_argument(
"screenshot-delay",
_("Screenshot dela_y"),
_("Delay before taking the screenshot"),
0,
20,
0,
GObject.ParamFlags.READWRITE,
)
procedure.add_image_return_value(
"image",
_("Image"),
_("Screenshot"),
False,
GObject.ParamFlags.READWRITE,
)
return procedure
Gimp.main(ScreenshotGrim.__gtype__, sys.argv)