pyautogui workaround for wayland - python

From what I understand, pyautogui doesn't work properly when trying to get screenshots of my screen because I'm running Ubuntu 22.04 wayland.
I saw somewhere that you could enable Xorg again and it should work fine... however, this solution is not ideal for me.
Anyone knows any workaround for pyautogui to be able to capture proper screenshot?
Below my code to get the screenshot, but when I check the "test.png" file, it's all black.
import pyautogui as pg
x1,y1 = 1525,312
x2,y2 = 1655,370
img = pg.screenshot(region=(x1,y1,x2,y2))
img.save("test.png")

Related

Use python to grab the screen(screenshot) But image is blurry, unsharp

Environment:
win10 PC notbook
python 3.9
PyQt5==5.15.4 pywin32==304 opencv-python==4.5.5.64 Pillow==9.1.0
Question:
I am trying to grab my whole screen by python. The function is easy, but I get a strange question.
The screenshot image is always blurry and unsharp.
screenshot by windows10 self: windows10 self (PNG)
screenshot by my codes: my codes (PNG)
My Codes as follows:
import cv2
import os
from PIL import ImageGrab
import numpy as np
if os.path.exists('xx.jpg'):
os.remove('xx.jpg')
captureImage = ImageGrab.grab()
width,height = captureImage.size
img = cv2.cvtColor(np.asarray(captureImage), cv2.COLOR_RGB2BGR)
cv2.imwrite('./xx.jpg',img)
cv2.waitKey(0)
I have try alternative method to achieve screenshot function, just like pyqt5/mss/pywin32/pyautogui, but every method leads to the same question.
And I google lots of articles, I found that my screen has scaled to 125%(as recommended), as follow:
PNG:
So I revise it to 100%, the screenshot is sharp. But 125% is normal and comfortable vision.
Then I doubt if it's DPI. But I try these codes, it does not work.
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
I am so crazy and curious that how those screenshot software resolve it. After all, we can not ensure every user has the 100% scale and has normal dpi.
I will be grateful if u could give me solutions or advices.
Solution:
Thanks to Christoph Rackwitz.
I had grab a PNG screen and update it and the two PNG image still same. But it's really different in my PC before upload.
I test a lot and I found that I open my codes screenshot by iQIYI Player. But I open windows10 self screenshot by windows Player.
It's the Reason!!!My codes is right and everything is right except iQIYI Player Software. Maybe it's the software bug.
Thanks again.

How to get pyautogui's click working on mac?

pyautogui's click method's issue:
I am running the script from Spyder, if I click anything on Spyder's window the click works fine.
If I execute a script to open Outlook, then click on anything, the click does not happen. Although I am able to use the "moveTo" functionality properly.
Things I have tried as suggested by doing google search:
pyautogui.click()
pyautogui.click()
OS : mac os high sierra
Note:
In order to reach any located image I have to do coordinates/2, as it is a Retina 2x display.
Any workaround or any help will be greatly appreciated.
To anyone who might stumble into the same issue on a Mac, I was able to get it working by using a workaround that is using the pynput library.
Code:
import pyautogui
from pynput.mouse import Button, Controller
mouse = Controller()
pyautogui.moveTo(x,y)
mouse.click(Button.left)
OS X Mojave, the following works for me:
pyautogui.moveTo(pos)
pyautogui.dragTo(button='left')
pyautogui.click() throws an attribute error but pyautogui.dragTo() works instead.
I just found out that in mac setting, I did not check the tip in front of PyCharm in privacy setting. After doing that, my pyautogui.click() function works.
I was also facing same issue, Here is what I tried :
Just add one more line pyautogui.dragTo() to focus on that particular selected area:
pyautogui.moveTo(990,28)
pyautogui.dragTo()
pyautogui.click()

pyautogui - Xlib.error.BadValue

I am using the pyautogui library, and it was working...and for some reason after I reinstalled my Ubuntu system I dont manage to use it.
I show below the code, just several lines copied from the Docs.
The only information about the error, showed in picture I found is this link mentioning "BadValue error", which I dont really understand (100 and 200 are inside range)
https://tronche.com/gui/x/xlib/event-handling/protocol-errors/default-handlers.html
import pyautogui
pyautogui.moveTo(100, 200)
pyautogui.position()
pyautogui.click()
pyautogui.click(100, 200)
Thanks a lot!
Picture with code and execution error
Well, I don't know why this error is coming, because in PyCharm, it works perfectly. But I noticed that the first 3 lines are unnecessary so try this code and tell me if it helps:
from pyautogui import *
click(100, 200)

pyautogui doubleClick not working on OSX

This is my first question so apologies in advanced for everything I'll do wrong in the next lines.
I am having issues with the doubleClick function from pyautogui on mac with OSX.
Lately I have written some python code using the following libraries
from time import sleep
from random import uniform as r
from pyautogui import click, doubleClick, press, moveTo, mouseDown, mouseUp
from webbrowser import open_new_tab
Even by trying the simplest combination
x1, y1 = (300, 375)
doubleClick(x=x1, y=y1)
there is no action taking place. I have tried two quick clicks with an interval, both also got no results from that.
I am experiencing this as a major issues and have checked now on several mac machines (5+) and the issue is always there and I cannot find any reference in other questions or on the internet.
Is anyone else experiencing the same?
Thank you already for your help.
Had the same problem. Found a fix here. (Just make sure you first activated the window you need to double click in, with a single click. I forgot and now it works perfectly.)

Pyautogui - Screenshot Doesn't Cover Entire Screen

I've been trying to use the screenshot feature of pyautogui, and whenever I take a screenshot it only captures the top left corner of the screen. Even when I manually enter a larger region to screenshot, it just makes the rest black.
Example:
What could I do to fix this?
Code that made this image:
import pyautogui
import time
import sys
im = pyautogui.screenshot('board.png',region=(0,0, 2000, 1000))
I found a work around from python-imaging-library-fails-to-grab-whole-screen, and at Github pyautogui Issues #116: Scaling issue on Windows affecting screenshots #116.
You still have to manually set the region as the default doesn't capture the whole screen.
import pyautogui
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
pyautogui.screenshot('my_screenshot.png', region=(0,0,1920,1080))

Categories