Python 屏幕截图

我正在尝试编写一个程序来使用全局热键进行屏幕截图。下面是对应的代码:


from datetime import datetime

import os

from pynput import keyboard

import pyautogui

import pathlib


def on_activate():

    today = datetime.now()

    d = today.strftime("%Y-%m-%d-%H-%M-%S")

    myScreenshot = pyautogui.screenshot(region=(200,200, 1720, 800))

    time_stamp = '{:%y%d%m}'.format(today)

    fpath = pathlib.Path("C:","Users","Desktop","TestScreenCap", time_stamp)

    if not os.path.exists(fpath):

        os.makedirs(fpath)

    myScreenshot.save(pathlib.Path(fpath,d,".jpg"))


def for_canonical(f):

    return lambda k: f(l.canonical(k))


hotkey = keyboard.HotKey(

    keyboard.HotKey.parse('<ctrl>+<alt>+h'),

    on_activate)

with keyboard.Listener(

        on_press=for_canonical(hotkey.press),

        on_release=for_canonical(hotkey.release)) as l:

    l.join()

错误信息:


Unhandled exception in listener callback

Traceback (most recent call last):

  File "C:\Users\PycharmProjects\untitled1\venv\lib\site-packages\PIL\Image.py", line 2138, in save format = EXTENSION[ext]

KeyError: ''


The above exception was the direct cause of the following exception:


Traceback (most recent call last):

  File "C:\Users\PycharmProjects\untitled1\venv\lib\site-packages\pynput\_util\__init__.py", line 211, in inner

    return f(self, *args, **kwargs)

  File "C:\Users\PycharmProjects\untitled1\venv\lib\site-packages\pynput\keyboard\_win32.py", line 280, in _process self.on_press(key)

  File "C:\Users\PycharmProjects\untitled1\venv\lib\site-packages\pynput\_util\__init__.py", line 127, in inner

    if f(*args) is False:

  File "C:/Users/PycharmProjects/untitled1/123.py", line 18, in <lambda>

    return lambda k: f(l.canonical(k))

  File "C:\Users\PycharmProjects\untitled1\venv\lib\site-packages\pynput\keyboard\__init__.py", line 182, in press

白衣非少年
浏览 141回答 1
1回答

Smart猫小萌

这里是固定代码:from datetime import datetimeimport osfrom pynput import keyboardimport pyautoguiimport pathlibdef on_activate():&nbsp; &nbsp; today = datetime.now()&nbsp; &nbsp; d = today.strftime("%Y-%m-%d-%H-%M-%S")&nbsp; &nbsp; myScreenshot = pyautogui.screenshot(region=(200,200, 1720, 800))&nbsp; &nbsp; time_stamp = '{:%y%d%m}'.format(today)&nbsp; &nbsp; fpath = pathlib.Path("C:\\","Users","Desktop","TestScreenCap", time_stamp)&nbsp; &nbsp; if not os.path.exists(fpath):&nbsp; &nbsp; &nbsp; &nbsp; os.makedirs(fpath)&nbsp; &nbsp; myScreenshot.save(pathlib.Path(fpath,d + ".jpg"))def for_canonical(f):&nbsp; &nbsp; return lambda k: f(l.canonical(k))hotkey = keyboard.HotKey(&nbsp; &nbsp; keyboard.HotKey.parse('<ctrl>+<alt>+h'),&nbsp; &nbsp; on_activate)with keyboard.Listener(&nbsp; &nbsp; &nbsp; &nbsp; on_press=for_canonical(hotkey.press),&nbsp; &nbsp; &nbsp; &nbsp; on_release=for_canonical(hotkey.release)) as l:&nbsp; &nbsp; l.join()问题是您正在使用:myScreenshot.save(pathlib.Path(fpath,d,".jpg"))该函数pathlib.Path(fpath,d,".jpg")将字符串添加\到每个字符串的末尾,因此您尝试保存文件的路径是:C:Users\Desktop\TestScreenCap\200309\2020-09-03-16-50-20\.jpg正如您所看到的,文件名是引发异常的扩展名。只需替换为:myScreenshot.save(pathlib.Path(fpath,d + ".jpg"))指定路径的另一个问题是它C:被解释为Drive,所以当你保存函数时,保存到你的程序当前运行的路径,而不是真正的指定路径。要修复此部分,只需替换C:为C:\\.阅读您的代码,我认为您希望将屏幕截图保存在当前用户桌面中,如果是这样,请将行:替换为fpath = pathlib.Path("C:\\","Users","Desktop","TestScreenCap", time_stamp)并fpath = pathlib.Path(os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop'), time_stamp)在顶部添加import os. 有了这个,你就不需要关心这条道路的正确世代。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python