python - 在 Ubuntu 上使用 python MSS 库时出现 XGetImage Failed 错误

标签 python linux x11 python-mss

我在 Ubuntu 18.10 上使用 mss 时遇到错误(如果有的话)。这是我的代码(取自文档中的示例,加上一些额外的错误输出):

import mss
with mss.mss() as sct:
    try:
        filename = sct.shot(mon=-1, output='fullscreen.png')
        print(filename)
    except Exception as e:
        print(e, "\n", e.details)

这是我使用 python 3.7 运行代码时看到的输出。

XGetImage() failed
 {'retval': <mss.linux.LP_XImage object at 0x7fc72d567bf8>, 'args': (<mss.linux.LP_Display object at 0x7fc72db7d1e0>, <mss.linux.LP_Display object at 0x7fc72d567950>, 0, 0, 1920, 1080, 16777215, 2)}

如果有帮助,这里是如果我没有捕获异常(堆栈跟踪)的输出

Traceback (most recent call last):
  File "process.py", line 3, in <module>
    filename = sct.shot(mon=-1, output='fullscreen.png')
  File "/home/user/.local/share/virtualenvs/poker-TAtsjijf/lib/python3.7/site-packages/mss/base.py", line 140, in shot
    return next(self.save(**kwargs))
  File "/home/user/.local/share/virtualenvs/poker-TAtsjijf/lib/python3.7/site-packages/mss/base.py", line 129, in save
    sct = self.grab(monitor)
  File "/home/user/.local/share/virtualenvs/poker-TAtsjijf/lib/python3.7/site-packages/mss/linux.py", line 430, in grab
    ZPIXMAP,
  File "/home/user/.local/share/virtualenvs/poker-TAtsjijf/lib/python3.7/site-packages/mss/linux.py", line 171, in validate
    raise ScreenShotError(err, details=details)
mss.exception.ScreenShotError: XGetImage() failed

我在虚拟环境中运行它,其中我的 Pipfile.lock 报告 mss 版本 4.0.2

我尝试以 root 身份运行它,但这给出了相同的输出。我在谷歌中找不到类似的问题。我也尝试在 tmux 之外运行它,但也没有结果。

最佳答案

我在使用Fedora 29时也遇到了同样的问题。但是在检查源代码后,似乎mss使用x11而不是xWayland 来捕获屏幕,xWayland 是 Fedora 中的默认设置。将 session 切换为使用 x11 解决了我的问题。切换方式提到here .

关于python - 在 Ubuntu 上使用 python MSS 库时出现 XGetImage Failed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55085064/

相关文章:

linux - 如何在linux中使用我自己的动态库(Makefile)

linux - 如何在 Unix 脚本中自动化并结束更多命令?

c++ - 从 xvfb 读取像素数据

gdb - xlib集成调试(Tracing)

python - 在每行满足另一个条件后查找第一行有条件的行

python - 为 jython 脚本创建可执行文件

linux - 在 Linux 中的 WAS 服务器中设置类路径

python - 相当于以下curl命令的python请求是什么

python - 如何根据从 python 字符串中提取的数值按降序对记录进行排序?

c - 使用 libx11 截图