Welcome folks today in this blog post we will be using the pyscreenshot
module in tkinter to capture the screenshot
and save it as png
image file. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries
using the below command as shown below
pip install pillow
pip install pyscreenshot
And after that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import os, time import tkinter as tk from datetime import datetime from functools import partial from io import BytesIO import win32clipboard from PIL import Image import pyscreenshot as ImageGrab # Create folder snips if it doesn't exist if not (os.path.isdir("snips")): os.mkdir("snips") # Create Tkinter root root = tk.Tk() root.title("Screen Grabber") # Main canvas canvas = tk.Canvas(root, width=500, height=250, bg="lightblue") copy_clip = tk.IntVar() show_clip = tk.IntVar() copy_clip_check = tk.Checkbutton(root, text="Copy screenshot to clipboard", variable=copy_clip) show_clip_check = tk.Checkbutton(root, text="Open screenshot after clip", variable=show_clip) # Get dimensions from user user_left = tk.Entry(root, text="0") canvas.create_window(100, 45, window=user_left) user_top = tk.Entry(root, text="100") canvas.create_window(100, 75, window=user_top) user_right = tk.Entry(root, text="500") canvas.create_window(100, 105, window=user_right) user_bottom = tk.Entry(root, text="400") canvas.create_window(100, 135, window=user_bottom) # Image copy status message label = tk.Label(root, text="", bg="lightblue", font=('helvetica', 10)) canvas.create_window(250, 230, window=label) dim_clip_button = tk.Button(text='Clip with Dimensions', command=partial(get_user_dimensions), bg="#8e936d", fg="black", padx=10, font=("Sans Serif", 9)) canvas.create_window(100, 190, window=dim_clip_button) copy_clip_check.pack() show_clip_check.pack() canvas.pack() root.mainloop() |
As you can see we are importing the tkinter
and pillow
library. And then we are making the snips
directory if that directory doesn’t exists. And then we have the canvas
element where we are capturing the screenshot
and then we have widgets
to take the full screen
screenshot and then we have the input
fields to allow user to enter the dimensions
of the screenshot. And then we have different buttons to take the screenshot
of the specified region or full size screenshot.
Get User Dimensions
Now we will be writing the function
to get the user
dimensions values such as the x
and y
coordinates and then we are getting the width and the height of the screenshot
to capture on the screen
1 2 3 4 5 6 |
def get_user_dimensions(): left = user_left.get().strip() top = user_top.get().strip() right = user_right.get().strip() bottom = user_bottom.get().strip() label.configure(text=clip_screen((left, top, right, bottom))) |
And now we will be writing the functions
to take the screenshot
of the specified regions and full size
screenshot as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
def prepare_to_copy_clipboard(file_path): image = Image.open(file_path) output = BytesIO() image.convert("RGB").save(output, "BMP") data = output.getvalue()[14:] output.close() send_to_clipboard(win32clipboard.CF_DIB, data) def send_to_clipboard(clip_type, data): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(clip_type, data) win32clipboard.CloseClipboard() def clip_screen(dimensions): root.withdraw() time.sleep(0.3) image_name = "snips/" + str(datetime.now()).replace(" ", "").replace(":", "") + ".png" try: if dimensions: image = ImageGrab.grab(bbox=tuple(map(int, dimensions))) else: image = ImageGrab.grab() image.save(image_name) if copy_clip.get(): prepare_to_copy_clipboard(image_name) if show_clip.get(): image.show() except Exception as e: print(e) return e root.deiconify() return "Screenshot saved as " + image_name def get_fullscreen(): label.configure(text=clip_screen(None)) |
As you can see we are using the pyscreenshot
module to take the screenshot
of the specified region and save the screenshots
inside the snips
folder as shown below
FULL SOURCE CODE
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import os, time import tkinter as tk from datetime import datetime from functools import partial from io import BytesIO import win32clipboard from PIL import Image import pyscreenshot as ImageGrab # Create folder snips if it doesn't exist if not (os.path.isdir("snips")): os.mkdir("snips") # Create Tkinter root root = tk.Tk() root.title("Screen Grabber") # Main canvas canvas = tk.Canvas(root, width=500, height=250, bg="lightblue") copy_clip = tk.IntVar() show_clip = tk.IntVar() copy_clip_check = tk.Checkbutton(root, text="Copy screenshot to clipboard", variable=copy_clip) show_clip_check = tk.Checkbutton(root, text="Open screenshot after clip", variable=show_clip) # Get dimensions from user user_left = tk.Entry(root, text="0") canvas.create_window(100, 45, window=user_left) user_top = tk.Entry(root, text="100") canvas.create_window(100, 75, window=user_top) user_right = tk.Entry(root, text="500") canvas.create_window(100, 105, window=user_right) user_bottom = tk.Entry(root, text="400") canvas.create_window(100, 135, window=user_bottom) # Image copy status message label = tk.Label(root, text="", bg="lightblue", font=('helvetica', 10)) canvas.create_window(250, 230, window=label) def prepare_to_copy_clipboard(file_path): image = Image.open(file_path) output = BytesIO() image.convert("RGB").save(output, "BMP") data = output.getvalue()[14:] output.close() send_to_clipboard(win32clipboard.CF_DIB, data) def send_to_clipboard(clip_type, data): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(clip_type, data) win32clipboard.CloseClipboard() def clip_screen(dimensions): root.withdraw() time.sleep(0.3) image_name = "snips/" + str(datetime.now()).replace(" ", "").replace(":", "") + ".png" try: if dimensions: image = ImageGrab.grab(bbox=tuple(map(int, dimensions))) else: image = ImageGrab.grab() image.save(image_name) if copy_clip.get(): prepare_to_copy_clipboard(image_name) if show_clip.get(): image.show() except Exception as e: print(e) return e root.deiconify() return "Screenshot saved as " + image_name def get_fullscreen(): label.configure(text=clip_screen(None)) fs_clip_button = tk.Button(text='Clip Full Screen', command=partial(get_fullscreen), bg="#8e936d", fg="black", padx=10, font=("Sans Serif", 9)) canvas.create_window(350, 110, window=fs_clip_button) def get_user_dimensions(): left = user_left.get().strip() top = user_top.get().strip() right = user_right.get().strip() bottom = user_bottom.get().strip() label.configure(text=clip_screen((left, top, right, bottom))) dim_clip_button = tk.Button(text='Clip with Dimensions', command=partial(get_user_dimensions), bg="#8e936d", fg="black", padx=10, font=("Sans Serif", 9)) canvas.create_window(100, 190, window=dim_clip_button) copy_clip_check.pack() show_clip_check.pack() canvas.pack() root.mainloop() |