How to screenshot on windows

Take Screenshots with python Tkinter

In this tutorial, we’ll guide you through the process of how to screenshot on windows by creating a Python application using the Tkinter library. It allows you to capture screenshots at regular intervals simply by clicking a button. This simple Tkinter application provides a user-friendly interface for configuring various settings and controlling the screenshot capture process. Follow the steps below to set up and use the provided code for the take_screenshot_loop application.

Step 1: Install Required Libraries

Before you start, ensure you have Python installed on your system. Additionally, install the required libraries using the following command in your terminal or command prompt:

pip install pillow tkinter

Step 2: How to screenshot on windows: Saving the Code

Copy the provided code and save it in a file, for example, screenshot_looper.py.

import time
import os
from PIL import ImageGrab
import tkinter as tk
from tkinter import ttk, messagebox

class TakeScreenshots:
    def __init__(self, master):
        self.master = master
        self.master.title("Take Screenshot Looper")
        self.master.geometry('300x450')
        self.create_widgets()

    def create_widgets(self):
        ttk.Label(self.master, text="Object File Name:").pack(pady=5)
        self.mob_entry = ttk.Entry(self.master)
        self.mob_entry.insert(0, "example")
        self.mob_entry.pack(pady=5)

        ttk.Label(self.master, text="Monitor (left, top, right, bottom):").pack(pady=5)
        self.monitor_entry = ttk.Entry(self.master)
        self.monitor_entry.insert(0, "40, 0, 800, 640")
        self.monitor_entry.pack(pady=5)

        ttk.Label(self.master, text="Display Time (seconds):").pack(pady=5)
        self.display_time_entry = ttk.Entry(self.master)
        self.display_time_entry.insert(0, "0.5")
        self.display_time_entry.pack(pady=5)

        ttk.Label(self.master, text="Initial Image Index:").pack(pady=5)
        self.img_entry = ttk.Entry(self.master)
        self.img_entry.insert(0, "0")
        self.img_entry.pack(pady=5)

        ttk.Label(self.master, text="Directory:").pack(pady=5)
        self.directory_entry = ttk.Entry(self.master)
        self.directory_entry.insert(0, "./datasets/osrs/")
        self.directory_entry.pack(pady=5)

        self.status_label = ttk.Label(self.master, text="")
        self.status_label.pack(pady=10)

        ttk.Button(self.master, text="Start", command=self.start_capture).pack(pady=5)
        ttk.Button(self.master, text="Stop", command=self.stop_capture, state=tk.DISABLED).pack(pady=5)

        self.img = 0
        self.is_capturing = False

    def ensure_dir(self):
        self.directory = self.directory_entry.get()
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

    def start_capture(self):
        self.status_label.config(text="Running", foreground='#8fce00')
        self.master.iconify()
        time.sleep(0.2)

        self.mob = self.mob_entry.get()
        monitor_values = list(map(int, self.monitor_entry.get().split(',')))
        self.monitor = tuple(monitor_values)
        self.display_time = float(self.display_time_entry.get())
        self.img = int(self.img_entry.get())
        self.directory = self.directory_entry.get()

        self.is_capturing = True
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
        self.capture_images()

    def stop_capture(self):
        self.status_label.config(text="Stopped", foreground='#f44336')
        self.is_capturing = False
        self.start_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)

    def capture_images(self):
        while self.is_capturing:
            try:
                im = ImageGrab.grab(bbox=self.monitor)
                im.save(os.path.join(self.directory, f'{self.mob}_{self.img}.jpg'))
                self.img += 1
                time.sleep(self.display_time)
                self.master.update()
            except Exception as e:
                messagebox.showerror("Error", str(e))
                self.is_capturing = False

if __name__ == "__main__":
    root = tk.Tk()
    app = TakeScreenshots(root)
    root.mainloop()

Step 3: Run the Application

Open a terminal or command prompt, navigate to the directory containing the saved script (screenshot_looper.py), and run the following command:

python screenshot_looper.py

Step 4: Configure Settings

Once the application window opens, you’ll see several Entry widgets to input the following settings:

  • Object File Name: The prefix for the screenshot filenames.
  • Monitor (left, top, right, bottom): Define the area on the screen to capture in the format left, top, right, bottom.
  • Display Time (seconds): Set the time interval between each screenshot.
  • Initial Image Index: The starting index for the screenshot filenames.
  • Directory: The directory where screenshots will be saved.

Step 5: Start and Stop Capture

Click the “Start” button, this is how to screenshot on windows which is to initiate the screenshot capture process with the specified settings. To stop capturing screenshots, click the “Stop” button.

How to take a screenshot in windows - the gui made with python

Step 6: Review Captured Screenshots

Navigate to the specified directory to find the captured screenshots. The filenames will be in the format {object_file_name}_{image_index}.jpg.

How to take a screenshot in windows - the output folder

Notes

  • If the specified directory does not exist, the application will create it.
  • If an error occurs during the capture process, an error message will be displayed.

Conclusion

This Tkinter application provides a convenient way on how to screenshot on windows by capturing screenshots at regular intervals, making it useful for various tasks such as creating datasets or monitoring visual changes over time. Customize the settings based on your specific requirements, and enjoy the ease of capturing screenshots with this user-friendly interface. Whether you’re a developer, data scientist, or simply someone who needs periodic screenshots, this application streamlines the process for you.

Want to know how the code works? TBA soon for tutorial on how the elements and functions work in this screenshot tkinker application.

Leave a Reply

Your email address will not be published. Required fields are marked *