Skip to content
Advertisement

Pyautogui taking screenshot of a region like sharex does

I am trying to make a program using pyautogui to screenshot a part of my screen. Everything is working as I intend it to, except when I click on the screenshot button, the only way I found to make it possible for me to choose the region is by doing this :

import pyautogui

print("Place your mouse in the top left corner of your region")
time.sleep(1)
pos1 = pyautogui.position()

print("Place your mouse in the bottom right corner of your region")
time.sleep(1)
pos2 = pyautogui.position()

width = pos2[0] - pos1[0]
height = pos2[1] - pos1[1]

imag = pyautogui.screenshot(region=(pos1[0], pos1[1], width, height))

# Save the screenshot to a file
imag.save("screenshot.png")

So here I essentially make the program wait for x second (1 second here) and I have to place my mouse quickly where I want, and do the same thing in x second for the other corner of the region. If I don’t do this, the screenshot is taken instantly without asking for coordinates

My question is : how do I make it work like the ShareX region screen ? i.e: when you click and hold my left mouse button, a box appears, I can expand it by moving the mouse around, and when the region is of my liking, I let go of the mouse button and the region is set for a screenshot.

Advertisement

Answer

If you are here wondering how to do that, future person, I managed to do exactly what I wanted using Tkinter for all the inputting part and pyautogui only to take the screenshot with tkinter’s data.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement