You may use the following template to take a screenshot using Python:
import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'Path to save screenshot\file name.png')
In the next section, I’ll show you the complete steps to take a screenshot using Python. I’ll also include the code to create a GUI to take screenshots:
To start, you’ll need to install the pyautogui package using the following command (under Windows):
pip install pyautogui
You may check this guide for the instructions to install a package using pip for Windows users.
Next, capture the full path where the screenshot will be saved on your computer.
In my case, I decided to save the screenshot under the following path:
C:\Users\Ron\Desktop\Test\screenshot1.png
Here, the file name is screenshot1, while the file type is png (alternatively, you can set the file type to jpg).
For the final step, you’ll need to use the template below in order to take the screenshot using Python:
import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'Path to save screenshot\file name.png')
For our example, I applied the following code to take the screenshot (note that you’ll need to change the path to reflect the location where the screenshot will be saved on your computer):
import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot1.png')
Run the code in Python (adjusted to your path), and the screenshot will saved at your specified location:
Now, I’ll share with you the code to create a GUI that will allow you to take a screenshot by clicking a button.
As before, you’ll need to adjust the path to reflect the location where the screenshot will be saved on your computer:
import pyautoguiimport tkinter as tkroot= tk.Tk()canvas1 = tk.Canvas(root, width = 300, height = 300)canvas1.pack()def takeScreenshot (): myScreenshot = pyautogui.screenshot() myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot2.png')myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)canvas1.create_window(150, 150, window=myButton)root.mainloop()
Run the code, and you’ll see this GUI:
Click on the button, and the screenshot will be saved at your specified location:
So far, you saw how to take a screenshot by specifying the path where the screenshot will be saved. But did you know that you could also take screenshots without specifying the path in the code?
You can definitely do that using the code below:
import pyautoguiimport tkinter as tkfrom tkinter import filedialogroot= tk.Tk()canvas1 = tk.Canvas(root, width = 300, height = 300)canvas1.pack()def takeScreenshot (): myScreenshot = pyautogui.screenshot() file_path = filedialog.asksaveasfilename(defaultextension='.png') myScreenshot.save(file_path)myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)canvas1.create_window(150, 150, window=myButton)root.mainloop()
Run the code, and you’ll get the following GUI:
Click on the button to take the screenshot, and you’ll see a dialogue box that will allow you to choose the location where the screenshot will be saved:
联系客服