Mr.R-Word
Mr.R-Word

Reputation: 21

pyautogui not saving screenshot to the directory i told it to

I am making a program to help me quickly take screenshots during school lectures.I told the pyautogui to save the screenshot to a directory but it doesnt save it there and saves it in the folder where the script is present.It also doesn't give any error.Just doesn't save it to the path where i want it to. My code:-

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()

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\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\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:

I get this error if i try to use double forward slashes or other modules such as os.path

Upvotes: 0

Views: 1373

Answers (1)

Shahriar Rahman Zahin
Shahriar Rahman Zahin

Reputation: 642

This is the code I'm using in my code which is working fine:

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

Try using name-value pairs like I have used imageFilename=. You don't have to take screenshot and save it in another line. For your case please try:

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

pyautogui.screenshot(imageFilename=image_loc)

Upvotes: 1

Related Questions