Skip to content

Commit 7360348

Browse files
committed
helper: Use pyscreenshot for cross-platform QR capture
pyscreenshot supports capturing screenshots via numerous backends and can automatically select the best one depending on platform. This enables capturing of the QR code on wayland-based desktops while maintaining support for Windows.
1 parent 5f407bb commit 7360348

File tree

3 files changed

+55
-135
lines changed

3 files changed

+55
-135
lines changed

helper/helper/qr.py

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mss
1615
import zxingcpp
1716
import base64
1817
import io
1918
import os
2019
import sys
2120
import subprocess # nosec
2221
import tempfile
23-
from mss.exception import ScreenShotError
22+
import pyscreenshot as ImageGrab
2423
from PIL import Image
25-
import numpy.core.multiarray # noqa
26-
27-
28-
def _capture_screen():
29-
try:
30-
with mss.mss() as sct:
31-
monitor = sct.monitors[0] # 0 is the special "all monitors" value.
32-
sct_img = sct.grab(monitor) # mss format
33-
return Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
34-
except ScreenShotError:
35-
# One common error is that mss doesn't work with Wayland
36-
if sys.platform.startswith("linux"):
37-
# Try calling screenshot tools, with original library path
38-
env = dict(os.environ)
39-
lp = env.get("LD_LIBRARY_PATH_ORIG")
40-
if lp is not None:
41-
env["LD_LIBRARY_PATH"] = lp
42-
else:
43-
env.pop("LD_LIBRARY_PATH", None)
44-
fd, fname = tempfile.mkstemp(suffix=".png")
45-
46-
try:
47-
# Try using gnome-screenshot
48-
rc = subprocess.call( # nosec
49-
["gnome-screenshot", "-f", fname], env=env
50-
)
51-
if rc == 0:
52-
return Image.open(fname)
53-
except FileNotFoundError:
54-
# Try using spectacle (KDE)
55-
try:
56-
rc = subprocess.call( # nosec
57-
["spectacle", "-b", "-n", "-o", fname], env=env
58-
)
59-
if rc == 0:
60-
return Image.open(fname)
61-
except FileNotFoundError:
62-
pass # Fall through to ValueError
63-
finally:
64-
os.unlink(fname)
65-
raise ValueError("Unable to capture screenshot")
6624

25+
import numpy.core.multiarray # noqa
6726

6827
def scan_qr(image_data=None):
6928
if image_data:
7029
msg = base64.b64decode(image_data)
7130
buf = io.BytesIO(msg)
7231
img = Image.open(buf)
7332
else:
74-
img = _capture_screen()
33+
img = ImageGrab.grab()
7534

7635
result = zxingcpp.read_barcode(img)
7736
if result and result.valid:

helper/poetry.lock

Lines changed: 50 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helper/pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "authenticator-helper"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "Yubico Authenticator Helper"
55
authors = ["Dain Nilsson <[email protected]>"]
66
packages = [
@@ -11,9 +11,8 @@ packages = [
1111
[tool.poetry.dependencies]
1212
python = "^3.8"
1313
yubikey-manager = "5.1.0"
14-
mss = "^8.0.3"
1514
zxing-cpp = "^2.0.0"
16-
Pillow = "^9.5.0"
15+
pyscreenshot = "^3.1"
1716

1817
[tool.poetry.dev-dependencies]
1918
pyinstaller = {version = "^5.10.1", python = "<3.12"}

0 commit comments

Comments
 (0)