Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save hotkey screenshot in pyautogui

I have taken a screenshot using hotkeys in pyautogui, as that way I was able to get only the window content. Now I don’t seem to be able to save it. Am I doing something wrong or is there any way to get the screenshot?

screenshot = pyautogui.hotkey('alt', 'printscreen')
screenshot.save('temp.jpg')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Exspression pyautogui.hotkey('alt', 'printscreen') retuns nothing, so you can’t save image from it.

Simplest solution I could find is to install some extra modules:

pip install pillow keyboard

Module keyboard is used to create smarter hotkey with python function as callback.

Such function shoul press Print Screen to save image to clipboard, then we can get image from clipboard with PIL module and save it to file.

Here is the example:

import time

import pyautogui
import keyboard

from PIL import ImageGrab

def save_screenshoot():
    pyautogui.press('printscreen')
    im = ImageGrab.grabclipboard()
    im.save('screenshoot.png','PNG')

keyboard.add_hotkey('alt', save_screenshoot)

while True:
    # While this loop is active 'alt' wil save screenshoot
    time.sleep(5)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading