Screenshot Functions¶
PyAutoGUI can take screenshots, save them to files, and locate images within the screen. This is useful if you have a small image of, say, a button that needs to be clicked and want to locate it on the screen. These features are provided by the PyScreeze module, which is installed with PyAutoGUI.
Screenshot functionality requires the Pillow module. OS X uses the screencapture command, which comes with the operating system. Linux uses the scrot command, which can be installed by running sudo apt-get install scrot .
The screenshot() Function¶
Calling screenshot() will return an Image object (see the Pillow or PIL module documentation for details). Passing a string of a filename will save the screenshot to a file as well as return it as an Image object.
On a 1920 x 1080 screen, the screenshot() function takes roughly 100 milliseconds — it’s not fast but it’s not slow.
There is also an optional region keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:
The Locate Functions¶
NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None .
You can visually locate something on the screen if you have an image file of it. For example, say the calculator app was running on your computer and looked like this:

You can’t call the moveTo() and click() functions if you don’t know the exact screen coordinates of where the calculator buttons are. The calculator can appear in a slightly different place each time it is launched, causing you to re-find the coordinates each time. However, if you have an image of the button, such as the image of the 7 button:

… you can call the locateOnScreen(‘calc7key.png’) function to get the screen coordinates. The return value is a 4-integer tuple: (left, top, width, height). This tuple can be passed to center() to get the X and Y coordinates at the center of this region. If the image can’t be found on the screen, locateOnScreen() raises ImageNotFoundException .
The optional confidence keyword argument specifies the accuracy with which the function should locate the image on screen. This is helpful in case the function is not able to locate an image due to negligible pixel differences:
Note: You need to have OpenCV installed for the confidence keyword to work.
The locateCenterOnScreen() function combines locateOnScreen() and center() :
On a 1920 x 1080 screen, the locate function calls take about 1 or 2 seconds. This may be too slow for action video games, but works for most purposes and applications.
There are several “locate” functions. They all start looking at the top-left corner of the screen (or image) and look to the right and then down. The arguments can either be a
- locateOnScreen(image, grayscale=False) — Returns (left, top, width, height) coordinate of first found instance of the image on the screen. Raises ImageNotFoundException if not found on the screen.
- locateCenterOnScreen(image, grayscale=False) — Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Raises ImageNotFoundException if not found on the screen.
- locateAllOnScreen(image, grayscale=False) — Returns a generator that yields (left, top, width, height) tuples for where the image is found on the screen.
- locate(needleImage, haystackImage, grayscale=False) — Returns (left, top, width, height) coordinate of first found instance of needleImage in haystackImage . Raises ImageNotFoundException if not found on the screen.
- locateAll(needleImage, haystackImage, grayscale=False) — Returns a generator that yields (left, top, width, height) tuples for where needleImage is found in haystackImage .
The “locate all” functions can be used in for loops or passed to list() :
These “locate” functions are fairly expensive; they can take a full second to run. The best way to speed them up is to pass a region argument (a 4-integer tuple of (left, top, width, height)) to only search a smaller region of the screen instead of the full screen:
Grayscale Matching¶
Optionally, you can pass grayscale=True to the locate functions to give a slight speedup (about 30%-ish). This desaturates the color from the images and screenshots, speeding up the locating but potentially causing false-positive matches.
Pixel Matching¶
To obtain the RGB color of a pixel in a screenshot, use the Image object’s getpixel() method:
Or as a single function, call the pixel() PyAutoGUI function, which is a wrapper for the previous calls:
If you just need to verify that a single pixel matches a given pixel, call the pixelMatchesColor() function, passing it the X coordinate, Y coordinate, and RGB tuple of the color it represents:
The optional tolerance keyword argument specifies how much each of the red, green, and blue values can vary while still matching:
Screenshot Taker with Python
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
What is PyAutoGUI?
PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications.
- 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.
Installing PyAutoGUI
Open your terminal and run the following command
Now that we have the package, we are ready to import it in our python script.
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 .
Now, let’s pack our Canvas using the pack method.
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 .
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 .
Now that our button is ready, let’s put it in the center.
At last, we will call our mainloop .
Our Python script is ready. Run the program and you will see something like this.
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.
How To Take A Screenshot In Python Using PIL
In this tutorial, I will demonstrate how to take a screenshot using PIL in Python. Window, macOS and Linux are all supported.
PIL (Python Imaging Library / Pillow) is a Python library that adds support for opening, manipulating, and saving many different image file formats. It has a very friendly API with lots of help online including good documentation.
PIL supports Windows macOS and Linux and supports many versions of Python; see the installation notes to identify which version of PIL you will need for the version of Python you are using. At the time of wiring this, PIL 7 has been released and supports Python 3.8 — 3.5.
To install PIL, execute the following in a terminal:
To install an older version of PIL, execute python -m pip install pip=<version> eg. python -m pip install pip=7.2.0 . You can find older versions released in the release notes.
To validate it was installed correctly, go to IDLE or a python shell and execute:
If no import error is raised, it was installed successfully.
I Can’t Import PIL
Make sure you have executed the install command above; if you’re not sure you have executed it already, it is safe to execute it a second time.
If there were no errors when installing it and it says PIL is installed successfully, make sure you’re 100% sure that you installed PIL in the same distribution of Python that you’re trying to import it in. Go to my tutorial on How to Manage Multiple Python Distributions if you’re having some issues or are unsure about this.
Taking A Screenshot
To take a screenshot, we first need to import the ImageGrab module from PIL.
After we have the ImageGrab module, we can call .grab() to take a screenshot
On Linux, you must be using PIL 7.1.0 or higher for this to work; see release notes.
Viewing The Screenshot
To view the screenshot, we can call .show() on the returned Image object. For example, using the code from above:
Executing this will open the screenshot in your default image viewer. If you have more than one display, you will notice that this screenshot is only of the first display; I will expand on this further below.
Here is an example of a screenshot I took:

Saving The Screenshot
Saving images in PIL is very easy, calling .save() on the returned Image object will allow us to save the image into a file.
Now if you go and open the file filepath («my_image.png» in the current working directory for this example), you will see the screenshot has been saved.
If you provide a file path with an extension of a supported image format, you can omit the format.
Saving To A File Object
If you have a file already open in write mode or want to save the image to a file object, you can pass that instead of the filename. For example:
Taking A Screenshot Of A Different Display
Back when we took a screenshot using ImageGrab.grab() , it only captured the main display. To take a screenshot of all displays, we can pass all_screens=True :
Please note that all_screens is currently only supported in Windows
Now when you call screenshot.show() , you will see that multiple monitors are now displayed. Here is an example of my monitors:

Now that you have this larger image, you can crop it using other methods in PIL: How to crop an image using PIL.
Using MSS to Screenshot Each Displays
I also have a tutorial How To Take A Screenshot In Python Using MSS which goes over how to take screenshots using the Python MSS library. The tutorial is very similar, however MSS has better support for screenshots of different individual displays (including macOS and Windows).
Owner of PyTutorials and creator of auto-py-to-exe. I enjoy making quick tutorials for people new to particular topics in Python and tools that help fix small things.
2 Ways to Capture Screenshots Using Python

In this tutorial, we will learn how can one take screenshots using the Python programming language. There are multiple ways to achieve the same, we will be discussing some of them in the coming sections.
How to capture screenshots using Python
Python offers various libraries to capture screenshots. We’ll be exploring a few of these libraries today and understand how you can implement the code in Python to capture your screens.
Method 1: Using pyautogui module
The pyautogui module makes use of the screenshot function which is responsible for taking the screenshot of the whole computer screen. And then the save function is used to save the screenshot captured to our device.
The image saved would look something like this.

Screenshot Python 1
If one wants some delay before taking a screenshot, the programmer can make use of the time module and making use of the sleep function.
Method 2: Using pillow module
The pillow module makes use of an ImageGrab submodule. This method requires a region that needs to be captured which implies setting the diagonal coordinates of the region.
Then we make use of the grab function which will take the region parameters to capture the screenshot. And finally, save the captured image using the save function.
The region captured is shown below. We can also make use of the time module to delay the capturing of the screenshot.

Screenshot Python 2
Conclusion
So now you know two methods to capture screenshots of your computer screen. And yes there are other methods as well as python is a very advanced language. Hope you liked reading it!