我使用 pyautogui 中的热键截取了屏幕截图,这样我只能获取窗口内容。现在看来是没办法挽回了。我做错了什么或者有什么方法可以获取屏幕截图吗?
screenshot = pyautogui.hotkey('alt', 'printscreen')
screenshot.save('temp.jpg')
最佳答案
表达式 pyautogui.hotkey('alt', 'printscreen')
不返回任何内容,因此您无法从中保存图像。
我能找到的最简单的解决方案是安装一些额外的模块:
pip 安装枕头键盘
模块keyboard
用于创建更智能的热键,并以Python函数作为回调。
这样的函数应该按Print Screen
将图像保存到剪贴板,然后我们可以使用PIL
模块从剪贴板获取图像并将其保存到文件。
示例如下:
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)
关于python - 在 pyautogui 中保存热键屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70841564/