How to Take a Screenshot Using Python?

In this blog post, we will be learning how to take a screenshot using Python. Python is a widely used high-level interpreted language that is known for its ease of use and readability. It has many modules that can be used for different purposes, including taking screenshots. We will be using the pyautogui module in this blog post. Pyautogui is a cross-platform GUI automation module that works on Windows, macOS, and Linux.

How to Take a Screenshot Using Python with PyAutoGUI?

Before we can start, we need to install the PyAutoGUI library. Luckily, this is pretty simple. We just need to open our command prompt and type in the following:

pip install pyautogui

This will automatically download and install PyAutoGUI for us. If you don’t have pip installed, head over to their website for instructions on how to download it.

Taking a screenshot is a simple process that can be accomplished in just a few lines of code. The first thing we need to do is import pyautogui. To do this, we will type the following into our Python IDE:

import pyautogui
Code language: JavaScript (javascript)

Next, we need to tell Python what part of the screen we want to take a screenshot of. We can do this by using the pyautogui.screenshot() function. This function takes two arguments: the first is the region of the screen that you want to take a screenshot of, and the second is an optional filename where you want to save the screenshot. The region argument is a 4-tuple that specifies the top, left, width, and height of the region. If no region is specified, then the entire screen will be captured.

For example, if we wanted to take a screenshot of the middle 50% of our screen, we would type the following:

pyautogui.screenshot('screenshot1.png', region=(0, 0, 0.5, 1))
Code language: JavaScript (javascript)

This would save our screenshot as ‘screenshot1.png’ in our current working directory.

If you don’t specify a filename, then pyautogui will save your screenshot as ‘screen_capture_[datetime].png’.

The datetime part will be replaced with the current date and time stamp.

Full source code, take a Screenshot Using Python

import time import pyautogui def screenshot(): name = int(round(time.time() * 1000)) name = '{}.png'.format(name) # To name the file time.sleep(5) # Time Wait Before Taking Screenshot img = pyautogui.screenshot(name) # To Show the Screenshot After Being Taken screenshot()
Code language: Python (python)

Download the Source Code here

Taking screenshots using Python is a quick and easy process that can be done with just a few lines of code. By import pyautogui and using the pyautogui.screenshot() function, you can take screenshots of your entire screen or just a specific region of it. Be sure to specify a filename if you don’t want your screenshot to be saved with the default name ‘screen_capture_[datetime].png’.

Take a Screenshot using Python with PyScreenshot

PyScreenshot module

The PyScreenshot module allows you to take screenshots of your screen or an individual window. It can be used on Windows,macOS, and Linux systems. The module is available on Github. To install it, use the command “pip install pyscreenshot”.

The module takes a screenshot of the entire screen by default. To take a screenshot of only the active window, use the command “screenshot = ImageGrab.grab()“. To take a screenshot of a specific region, use the command “screenshot = ImageGrab.grab(bbox=(x, y, width, height))”. The bbox parameter is the bounding box for the screenshot region.

To save the screenshot, use the command “‘filename’)”. The filename parameter is the name of the file to save the screenshot as. The file format will be determined by the file extension you provide. For example, if you provide a “.png” extension, the file will be saved as a PNG image.

Full Source Code

import pyscreenshot # im=pyscreenshot.grab(bbox=(x1,x2,y1,y2)) image = pyscreenshot.grab(bbox=(10, 10, 500, 500)) # To view the screenshot # To save the screenshot"pc-savvy.png")
Code language: PHP (php)

Here we will learn the simplest way of taking a screenshot using pyscreenshot module. Here we will use the function show() to view the screenshot. 

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts