[英]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.我在 pyautogui 中使用热键截取了屏幕截图,因为这样我只能获得 window 内容。 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')
Exspression pyautogui.hotkey('alt', 'printscreen')
retuns nothing, so you can't save image from it.表达式
pyautogui.hotkey('alt', 'printscreen')
不返回任何内容,因此您无法从中保存图像。
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.模块
keyboard
用于创建更智能的热键 python function 作为回调。
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.此类 function 应按
Print Screen
将图像保存到剪贴板,然后我们可以使用PIL
模块从剪贴板中获取图像并将其保存到文件中。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.