【问题标题】:pyautogui not saving screenshot to the directory i told it topyautogui 没有将屏幕截图保存到我告诉它的目录
【发布时间】:2020-08-20 17:46:39
【问题描述】:

我正在制作一个程序来帮助我在学校讲座期间快速截取屏幕截图。我告诉 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 行,保存 格式=扩展[分机] 关键错误:''

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

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

【问题讨论】:

  • 试试说myScreenshot.save(r"C://Users//Hp//Desktop//Maheer//Study//Chemistry") ?并尝试删除r
  • 试过了,但是报错了
  • os 模块可能有一些选择路径的方法?我相信你进入道路的方式是错误的。尝试使用filedialog 询问目录?
  • 我之前做过filedialog,它工作正常,但这个程序的重点是截取屏幕截图并立即保存,而无需我进入带有文件对话框的文件夹并保存
  • 那你输入路径的方式有问题,我猜

标签: python pyautogui


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多