pyautogui未将屏幕截图保存到我告诉它的目录

2024-04-25 00:21:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序来帮助我在学校讲课时快速截图。我告诉pyautogui将截图保存到一个目录中,但它没有保存在那里,而是保存在脚本所在的文件夹中。它也没有给出任何错误。只是没有将截图保存到我想要的路径。 我的密码:-

import tkinter as tk
import pyautogui
import keyboard
from datetime import date
from tkinter import filedialog

r = tk.Tk()
r.geometry("600x300")
r.configure(background="white")

def startss():
    while True:
        if keyboard.is_pressed("insert"):
            with open("chemistry.txt","r") as a:
                b = a.read()
            with open("chemistry.txt","w") as c:
                newval = int(b) + 1
                c.write(str(newval))
            todaydate = date.today()
            name = "Chemistry-" + str(b) + "-" + str(todaydate) + ".jpeg"
            myScreenshot = pyautogui.screenshot(name)
            myScreenshot.save(r"C:\Users\Hp\Desktop\Maheer\Study\Chemistry")

heading = tk.Label(r,text="Study-Screenshotter",fg="grey",bg="white",font="Arial 20 bold")
heading.place(relx=0.5,rely=0,anchor="n") 

ctext = tk.Label(r,text="Chemistry=F1",fg="black",bg="white",font="Arial 15 bold")
ctext.place(relx=0.11,rely=0.25,anchor="n") 

btext = tk.Label(r,text="Biology=F2",fg="black",bg="white",font="Arial 15 bold")
btext.place(relx=0.9,rely=0.25,anchor="n")

ptext = tk.Label(r,text="Physics=F3",fg="black",bg="white",font="Arial 15 bold")
ptext.place(relx=0.1,rely=0.45,anchor="n")

mtext = tk.Label(r,text="Maths=F4",fg="black",bg="white",font="Arial 15 bold")
mtext.place(relx=0.9,rely=0.45,anchor="n")

etext = tk.Label(r,text="English=F4",fg="black",bg="white",font="Arial 15 bold")
etext.place(relx=0.5,rely=0.65,anchor="n")

startbutton = tk.Button(r,text="Start",fg="black",bg="white",font="Arial 15 bold",command=startss)
startbutton.place(relx=0.5,rely=0.8,anchor="n")





r.mainloop()

Tkinter回调中的异常 回溯(最近一次呼叫最后一次): 文件“C:\Users\Hp\AppData\Local\Programs\Python37-32\lib\site packages\PIL\Image.py”,第2138行,保存 格式=扩展名[ext] 密钥错误:“”

上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "c:\Users\Hp\Desktop\Maheer\Study\Study-Screenshotter.py", line 25, in startss
    myScreenshot.save(rpath)
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 2140, in save
    raise ValueError("unknown file extension: {}".format(ext)) from e
ValueError: unknown file extension:

如果尝试使用双正斜杠或其他模块(如os.path),则会出现此错误


Tags: textimportplacelabeltkbgblackwhite
1条回答
网友
1楼 · 发布于 2024-04-25 00:21:37

这是我在代码中使用的代码,运行良好:

pyautogui.screenshot(imageFilename=r"H:\some\location\image.PNG"),
                     region=(Coordinate_X, Coordinate_Y, 300, 50))

尝试使用名称-值对,就像我使用的imageFilename=。你不必把截图保存在另一行。对于您的情况,请尝试:

image_loc = r'C:\Users\Hp\Desktop\Maheer\Study\Chemistry\' + name

pyautogui.screenshot(imageFilename=image_loc)

相关问题 更多 >