Pyautogui - Screenshot Doesn't Cover Entire Screen - python

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))

Related

pyautogui workaround for wayland

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")

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.

Pyautogui not properly moving the mouse in Roblox

I'm writing code in python to help me accomplish something in a Roblox game. I won't go into too much detail, but basically, the objective is to automatically click at certain points in the screen. To try to accomplish this, I am using pyautogui.click(). So for instance if the point I needed to click at was (300, 500), I'd do pyautogui.click(300, 500)
Outside of Roblox, pyautogui works just fine. However, whenever I have Roblox open and I use the commands, it doesn't work properly. So, let's say the mouse starts out at (0, 0), and I activate a click at (300, 500). When that happens, the mouse cursor doesn't move, and the click happens at (0, 0) where the mouse originally was. However, as soon as I move my actual mouse even slightly, the mouse cursor teleports back to (300, 500) where it was supposed to be.
This makes it impossible to do what I want because I want the program to click at certain spots without me having to move my actual, physical mouse. Does anyone know how I can use python to actually move the mouse properly?
You have to use win32api and win32con for moving and clicking the mouse, as pyautogui is sometimes slow, so I am using win32api instead.
This is the code I used.
from pyautogui import *
import pyautogui
import time
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.1) #uses time api, to simulate normal input.
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
you have to use pyautoit to make it work inside of roblox, i used autoit.mouse_click("left",x, y) to make the mouse go to an x and y position in your screen and click the left or right click.
Yes I've been getting the same problem so I switched up to AHK(Auto Hotkey). I think (Idk what's happening in the background) the win32 api calls made by python is not using a deep method as you have to download and give permission to AHK to achieve that. So Roblox might have found a way to detect physical input tho they still can't detect deep api calls made by AHK

How to make a CAPS LOCK indicator using PYTHON on Windows

i wanted to make a caps-lock and num-lock indicator using python on windows,
but i dont know how to start with and what all modules and library i need to implement my desired output.
THE OUTPUT i desire is that whenever i press CAPS-LOCK the borders of screen should go green and for *NUM-LOCK** blue.
also, does it require a GUI?
(but i want it to be transparent. i don't want minimise,close and maximise buttons on gui) and this all process should be done in background.
Please guide me on how should i approach this.
THANKS A LOT.
There's the WinApi package for python here.
Not very well documented, but after quick look at demos looks like this code works:
import win32api
import win32con
print(win32api.GetKeyState(win32con.VK_CAPITAL))
For green frame you'll need probably some GUI library (GTK+, Qt), but I don't know which one could achieve such effect.
Edit: I figured you can actually use the same WinApi to draw on the screen.
import win32api
import win32gui
import win32con
pen = win32gui.CreatePen(win32con.PS_SOLID, 7, win32api.RGB(255, 0, 0))
dc = win32gui.GetDC(0)
win32gui.SelectObject(dc, pen)
win32gui.MoveToEx(dc, 0, 0)
win32gui.LineTo(dc, 1920, 0)

Error while setting a bgpic with turtle

I'm new to the turtle module and I'm having a problem while setting a background pic for my turtle project.
when running this code:
import turtle as tr
import os
os.chdir(pathname)
tr.setup(400,400)
tr.bgpic("diamond.gif")
I get an error message for the 5th line ending with:
_tkinter.TclError: image "pyimage4" doesn't exist
Sometimes it's pyimage2 doesn't exist or pyimage36. At each execution it changes.
I didn't find a real solution in other posts. Any help will be much appreciated.
You're not showing us your actual minimal code that fails as your example doesn't get past this line:
os.chdir(pathname)
since pathname isn't defined. I downloaded this GIF, renamed it diamond.gif, and ran the following subset of your code:
import turtle as tr
tr.setup(400, 400)
tr.bgpic("diamond.gif")
tr.done()
This displays the GIF in a window:
If you repeat what I did, and it works, then this may be a problem with your GIF file. (Download the GIF from this link, don't use my PNG illustration above.) If you repeat what I did and it doesn't work, then it may be a problem with your environment. This error message:
_tkinter.TclError: image "pyimage4" doesn't exist
is often associated with independently initializing both the turtle and the tkinter modules. If you aren't doing such, perhaps you're running in a specially tweaked environment that is. There may be a workaround, but you first need to determine what's really happening.

Categories