这是 Wayland 的一项安全功能

这是 Wayland 的一项安全功能

我正在测试皮奥图吉在两台不同的计算机上运行 Ubuntu 22.04 操作系统。两台计算机都出现了相同的行为。
当我使用print(pt.screenshot('foo.png'))它时会创建一个黑色foo.png文件。

我是第一次使用这个库。也许有些针对 linux/Ubuntu 22.04 的配置我没有注意到。就好像代码无法访问屏幕(我不知道),需要一些权限。

这是我用来测试的 Python 代码:

# https://pyautogui.readthedocs.io/en/latest/quickstart.html

print('======== pyautogui Tests for Ubuntu 22.04 ========')
import cv2 as cv
print('OpenCV Path:', cv.__path__) # OpenCV Path: ['/home/xxx/.local/lib/python3.10/site-packages/cv2']

print('OpenCV Version:', cv.__version__) # OpenCV Version: 4.6.0

print('\n')

from time import sleep
# Waiting time
print('Waiting 5s')
sleep(5)

import pyautogui as pt

# Set up a 2.5 second pause after each PyAutoGUI call
# print('pt.PAUSE = 5')
# print('Set up a 5 second pause after each PyAutoGUI call')
# pt.PAUSE = 5
# print('Not Working. Need to setup sleep(5)')
# print('\n')

# Moving the mouse to the upper-left will raise a pyautogui.FailSafeException that can abort your program:
# print('pt.FAILSAFE = True')
# print('Moving the mouse to the upper-left will raise a pyautogui.FailSafeException that can abort your program')
# pt.FAILSAFE = True
# print('Not Working')
# print('\n')

# current mouse x and y
position = pt.position()  
print('Current mouse x and y')
print('position: ', position)
print('\n')

# Current screen resolution width and height
# screenSize = pt.size()  
# print('Current screen resolution width and height')
# print('screenSize: ', screenSize) # Size(width=1920, height=1080)
# print('\n')

# True if x & y are within the screen.
# print('True if x & y are within the screen')
# x = 200
# y = 400
# print('x: ', x)
# print('y: ', y)
# withinScreen = pt.onScreen(x, y)
# print('withinScreen-True: ', withinScreen)   # True
# x = 2000
# y = 4000
# print('x: ', x)
# print('y: ', y)
# withinScreen = pt.onScreen(x, y)
# print('withinScreen-False: ', withinScreen)  # False
# print('\n')

print('================ Mouse Functions ================')
# Move mouse to XY coordinates over num_second seconds
print('Move mouse to XY coordinates over num_second seconds')
num_seconds = 1
x = 200
y = 400
print('x: ', x)
print('y: ', y)
print('duration: ', num_seconds)
pt.moveTo(x, y, duration=num_seconds)  
# Result: we cant see the mouse moving in the screen but It seems like a invisible mouse - selects files in VSCODE
print('\n')

# move mouse relative to its current position
# print('move mouse relative to its current position')
# xOffset = 20
# yOffset = 40
# print('xOffset: ', xOffset)
# print('yOffset: ', yOffset)
# print('duration: ', num_seconds)
# pt.moveRel(xOffset, yOffset, duration=num_seconds)  
# print('\n')

while True:
    posXY = pt.position()
    print(posXY)
    # print(posXY[0])
    # print(posXY[1])
    # print(posXY,pt.pixel(posXY[0],posXY[1]))
    sleep(1)
    print('****')
    print('LocateCenter test2', pt.locateCenterOnScreen('./test2.png', confidence=0.7))
    print('Locate test2', pt.locateOnScreen('./test2.png', confidence=0.7))
    print('LocateCenter test3', pt.locateCenterOnScreen('./test3.png', confidence=0.7))
    print('Locate test3', pt.locateOnScreen('./test3.png', confidence=0.7))
    print('****')
    print('\n')
    if posXY[0] == 0:
        break

print('')
print('pt.screenshot()', pt.screenshot())
print(pt.screenshot('foo.png'))
print('================ Screen Functions ================')
print('Home Folder Coordinates')
print(pt.locateCenterOnScreen('./test2.png', confidence=0.4))

这是生成文件的行的输出(循环之后):

print('pt.screenshot()', pt.screenshot())
pt.screenshot() <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1920x1080 at 0x7F68E2805870>
    
print(pt.screenshot('foo.png'))
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1920x1080 at 0x7F68E28059F0>

这是我的 pip3 列表:

$ pip3 list

Package                Version
---------------------- --------------
absl-py                1.1.0
apturl                 0.5.2
asgiref                3.5.2
attrs                  21.4.0
blinker                1.4
Brlapi                 0.8.3
certifi                2020.6.20
chardet                4.0.0
click                  8.0.3
colorama               0.4.4
command-not-found      0.3
cryptography           3.4.8
cssselect2             0.6.0
cupshelpers            1.0
cycler                 0.11.0
dbus-python            1.2.18
defer                  1.0.6
distlib                0.3.4
distro                 1.7.0
distro-info            1.1build1
Django                 4.0.6
django-embed-video     1.4.4
easy-thumbnails        2.8.1
filelock               3.7.1
fonttools              4.33.3
gpg                    1.16.0-unknown
httplib2               0.20.2
idna                   3.3
importlib-metadata     4.6.4
jeepney                0.7.1
keyring                23.5.0
kiwisolver             1.4.3
language-selector      0.1
launchpadlib           1.10.16
lazr.restfulclient     0.14.4
lazr.uri               1.0.6
louis                  3.20.0
lxml                   4.9.1
macaroonbakery         1.3.1
matplotlib             3.5.2
mediapipe              0.8.10
more-itertools         8.10.0
MouseInfo              0.1.3
netifaces              0.11.0
numpy                  1.22.4
oauthlib               3.2.0
olefile                0.46
opencv-contrib-python  4.6.0.66
opencv-python          4.6.0.66
packaging              21.3
paperclip              2.6.0
pexpect                4.8.0
Pillow                 9.0.1
pip                    22.0.2
platformdirs           2.5.2
protobuf               3.12.4
ptyprocess             0.7.0
PyAutoGUI              0.9.53
pycairo                1.20.1
pycups                 2.0.1
PyGetWindow            0.0.9
PyGObject              3.42.0
PyJWT                  2.3.0
pymacaroons            0.13.0
PyMsgBox               1.0.9
PyNaCl                 1.5.0
pyparsing              2.4.7
pyperclip              1.8.2
PyQt5                  5.15.6
PyQt5-sip              12.9.1
PyRect                 0.2.0
pyRFC3339              1.1
PyScreeze              0.1.28
PySocks                1.7.1
python-apt             2.3.0+ubuntu2
python-dateutil        2.8.1
python-debian          0.1.43ubuntu1
python3-xlib           0.15
pytweening             1.0.4
pytz                   2022.1
pyxdg                  0.27
PyYAML                 5.4.1
reportlab              3.6.8
requests               2.25.1
SecretStorage          3.3.1
setuptools             59.6.0
six                    1.16.0
sqlparse               0.4.2
svglib                 1.3.0
systemd-python         234
tinycss2               1.1.1
torbrowser-launcher    0.3.3
ubuntu-advantage-tools 27.8
ubuntu-drivers-common  0.0.0
ufw                    0.36.1
unattended-upgrades    0.1
urllib3                1.26.5
virtualenv             20.15.1
wadllib                1.3.6
webencodings           0.5.1
wheel                  0.37.1
xdg                    5
xkit                   0.0.0
zipp                   1.0.0

这是我的操作系统/Python:

$ python3 -V
Python 3.10.4


$ scrot -v
scrot version 1.7

# Tkinter
$ tclsh
% info patchlevel
8.6.12

$ apt list --installed
tk8.6-blt2.5/jammy,now 2.5.3+dfsg-4.1build2 amd64 [installed,automatic]
tk8.6/jammy,now 8.6.12-1build1 amd64 [installed,automatic]
tk/jammy,now 8.6.11+1build2 amd64 [installed,automatic]

附加其正在生成的文件 foo.png。 在此处输入图片描述

它应该是以下两个所附屏幕之一:在此处输入图片描述或者在此处输入图片描述

知道那是什么或者我遗漏了什么吗?

答案1

这是 Wayland 的一项安全功能

Wayland 的其中一个特点是其安全设计,这有助于保护用户免受恶意应用程序的侵害。应用程序再也无法看到屏幕上的所有内容并监视您。但这也意味着您无法运行常见应用程序(如 shutter 或 gtk-recordmydesktop)并使用它来截取桌面的屏幕截图或截屏视频 - 它只能看到自己的窗口,而看不到其他任何内容(否则可能会立即崩溃)。需要使用系统(受信任)应用程序来执行这些操作。

来源:https://support.hubstaff.com/screenshot-capture-support-wayland-linux/

切换回 Xorg

重启 Ubuntu 系统。在登录屏幕的密码字段下,您会看到一个齿轮图标。只需单击它,您就会看到两个选项。

在此处输入图片描述

在 Ubuntu 22.04 LTS 中,默认Ubuntu是 Wayland 显示服务器。选择Xorg 上的 Ubuntu然后输入密码并点击Enter。这会将显示服务器从 Wayland 切换到 Xorg,并且您的截图程序应该可以正常工作。

参考:https://itsfoss.com/switch-xorg-wayland/

希望这可以帮助

相关内容