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

时间:2020-08-09 13:02:48

标签: python pyautogui

我正在制作一个程序来帮助我在学校上课时快速截取屏幕截图。我告诉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 \ Python \ Python37-32 \ lib \ site-packages \ PIL \ Image.py”,行2138 格式= EXTENSION [ext] KeyError:“

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

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),则会收到此错误

1 个答案:

答案 0 :(得分:1)

这是我在代码中正常使用的代码:

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)