I am testing the Pyautogui on a OS Ubuntu 22.04 in 2 different computers. Both ate presenting the same behavior.
When I use print(pt.screenshot('foo.png'))
it is creating a black foo.png
file.
I am new using this library. Maybe there are some configuration for linux/Ubuntu 22.04 that is escaping from my attention. It is like if the code would not have access to the screen (I dont know), some permission.
This is the python code I am using to test:
# 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))
This is the output from the lines that generate the file (just after the loop):
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>
This is my pip3 list:
$ 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
And this is my OS/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]
Attached the file foo.png it is generating.
It should be one of these 2 screens attached: or
Some Idea what that could that be or what I am missing?
It is a security feature of Wayland
Source: https://support.hubstaff.com/screenshot-capture-support-wayland-linux/
Switch back to Xorg
Restart you Ubuntu system. At the login screen, under the password field, you’ll see a gear icon. Just click on it and you’ll see two options here.
In Ubuntu 22.04 LTS, the default Ubuntu is Wayland display server. Select Ubuntu on Xorg before you enter your password and hit Enter. This will switch the display server from Wayland to Xorg and your screenshot program should work.
Reference: https://itsfoss.com/switch-xorg-wayland/
Hope this helps