Reputation: 51
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')
Upvotes: 2
Views: 1161
Reputation: 2554
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)
Upvotes: 2