Skip to main content

Command Palette

Search for a command to run...

Screenshot Taker with Python

Published
4 min read
Screenshot Taker with Python
A

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | connect with me on Twitter

In this Blog article, we will see how to Capture a Screenshot . We will see the implementation in python.

Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video tutorial for better Understanding

Introduction

Screenshot, also known as screen capture, is a digital image that shows the contents of a computer display. A common screenshot is created by the operating system or software running on the device. Taking, saving, and sharing screenshots can be extremely helpful.

What will be covered in this Blog

1. PyAutoGUI Introduction
2. How to capture Screenshot using python

What is PyAutoGUI?

PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications.

Features

  • Moving the mouse and clicking or typing in the windows of other applications.
  • Sending keystrokes to applications (for example, to fill out forms).
  • Take screenshots, and given an image (for example, of a button or checkbox), find it on the screen.
  • Locate an application’s window, and move, resize, maximize, minimize, or close it (Windows-only, currently)
  • Display message boxes for user interaction while your GUI automation script runs.

If you wish to know more about it, you can refer to PyAutoGUI Documentation. Use this link to navigate to the documentation.

Now that you are aware of PyAutoGUI basics, we can move forward to the coding section.

Time to code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

carbon (2).png

Installing PyAutoGUI

Open your terminal and run the following command

pip install PyAutoGUI

Now that we have the package, we are ready to import it in our python script.

import pyautogui
import tkinter as tk

Now, its time to create a canvas. Let's give it a title. I am setting Sc taker as the title using the title method. You can specify the canvas dimensions. I am setting the height and width as 300.

root= tk.Tk()
root.title('Sc taker')
canvas1 = tk.Canvas(root, width = 300, height = 300)

Now, let's pack our Canvas using the pack method.

canvas1.pack()

Now that we have our canvas ready, lets define a function to capture screenshot. I am naming it as myScreenshot.

We need to create an object to store our screenshot. I am naming it as sc. So, we will call screenshot method from PyAutoGUI on sc.

def myScreenshot():
  myScreenshot = pyautogui.screenshot()

  myScreenshot.save(r'C:\Users\imar\Desktop\Python Tutotial\screenshot1.png')

Let's save our screenshot using the save method,. Give proper path along with the screenshot name.

Now, let's create a button and specify few parameters.

  • I am setting the text of the button as Take Screenshot.
  • I am passing my function myScreenshot in command.
  • Background and foreground color as green and white respectively.
  • And finally, I am setting the font as 15.
myButton = tk.Button(text='Take Screenshot', command=myScreenshot, bg='green',fg='white',font= 15)

Now that our button is ready, let's put it in the center.

canvas1.create_window(150, 150, window=myButton)

At last, we will call our mainloop.

root.mainloop()

Our Python script is ready. Run the program and you will see something like this.

Screenshot_1.png

When you click the Take Screenshot button. A new screenshot will be captured and saved in your specified path.

That's it! simple, isn't it?

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter.

Do share your valuable feedback and suggestions!

You should definitely check out my other Blogs:

Resources:

  • https://pyautogui.readthedocs.io/en/latest/

See you in my next Blog article, Take care

S

PyAutoGUI is the best python module I have ever used. I am a Python programmer and have used many python modules like Sklearn, tkinter, pandas, numpy, matplotlib and many more but found nothing more interesting than PyAutoGUI

2
A

So good to hear from you Sannidhya Dasgupta.

I am still learning and exploring PyAutoGUI and other Python Modules.

S

That's great. If you face any kind of problems or have any questions feel free to ask meAyushi Rawat

2
S

Wow loved it! I always open your articles with interest to learn something new.

3
A

Thank you Laasya. 😄

1
S

Thank you very much for this beautiful education. God will bless you. Extra note: You can also screenshot without specifing path name. You can do that by, From tkinter import filedialog

Sc = pyautogui.screenshot() File_path = filedialog.asksaveasfilename(defaultextension = '.png') Sc.save(file_path). Thanks. I will be waiting for your next uploads and I have subscribed to your newsletter.

2
A

Thank you 😄 Yes, that is one great method.

S

any plans on making beautiful twitter screenshot maker?

D

Thanks for sharing the knowledge Ayushi ! Always learning something new from you !!

12
A

Thankyou Abhishek. Keep learning.

2
D

Thanks!!

1
D

Hi Ayushi,I was running the code and it was really great to see this solution of taking screen shots , just one thing, can we make it more dynamic for example, if i want to take second screen shot it should get saved in the same location with the name "screenshot2" without changing the code... Thanks again!!

1
A

Abhishek Mani Tripathi I will check and let you know.

1
D

Thanks Ayushi!!

1
P

You can add timestamp along with screenshot name using calender or time module, now you can create unique filename for every screenshot example screenshot-2020-10-19-646438.png Abhishek Mani Tripathi

2
D

Hi Praful, Can you please show the coding part of it how i can add in the existing code. Thanks again! Prafful Lachhwani

1

More from this blog

A

Ayushi Rawat - The official blog of Ayushi Rawat

72 posts

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | Connect with me on Twitter