AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1417704
Accepted
IgorAlves
IgorAlves
Asked: 2022-07-08 15:54:00 +0800 CST2022-07-08 15:54:00 +0800 CST 2022-07-08 15:54:00 +0800 CST

Pyautogui.screenshot() 在 Ubuntu 22.04 上返回黑色 file.png:。如何解决这个问题?

  • 772

我正在两台不同的计算机上测试操作系统 Ubuntu 22.04 上的Pyautogui 。两个人吃都表现出相同的行为。
当我使用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。 在此处输入图像描述

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

一些想法那可能是什么或我错过了什么?

python
  • 1 1 个回答
  • 280 Views

1 个回答

  • Voted
  1. Best Answer
    user68186
    2022-07-09T05:37:54+08:002022-07-09T05:37:54+08:00

    这是 Wayland 的安全功能

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

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

    切换回 Xorg

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

    在此处输入图像描述

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

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

    希望这可以帮助

    • 1

相关问题

  • 默认的字符编码是什么?

  • 如何使用 pynotify 创建可点击通知?

  • 有没有安装 Django 1.2.*(最新稳定版)的简单方法?

  • 为 Python 应用程序设置构建系统

  • 为我的 PPA 创建包时遇到问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve