Skip to main content

How To Take A Screenshot Using Python

A screenshot is nothing but an image of the data displayed on the screen of a mobile or computer device whereas a photograph is an image captured using the camera of a mobile device or a computer. The screenshot is also termed as screen capture or screengrab.

In this case, we will be using "pyautogui" module, which controls mouse and keyboard actions to take screenshots using python.


Installation

To install pyautogui module, use
pip install pyautogui
To verify whether the module is installed or not, use the command as
pip list
On specifying this command, there comes a list of modules installed on your device, where one can crosscheck whether the required module is installed on your device.
To view built-in functions of pyautogui module, use
dir(pyautogui)
This command lists out some of the built-in functions present inside this particular module.

Built-in functions in pyautogui module

screenshot( )

The screenshot( ) in pyautogui module simply returns an Image file that was captured as a screenshot on a device.

Syntax:

import pyautogui
image = pyautogui.screenshot( )
or
image = pyautogui.screenshot("filename.extension")
where the filename of the image was passed as an argument and specifying the extension of the image file is mandatory.
In some cases, we don't need to capture screenshots of the entire screen, only a specific portion of the screen is required, for that use,
image = pyautogui.screenshot(region=(0 , 0 , 100 , 300))

save( )

The save( ) function in pyautogui module, saves the image file in the specified location. The path to save the image file is passed as an argument inside the save( ) function.

Example

image.save(r"D:\filename.jpg")
where the filename specified in the argument is the filename of the image to be stored in the destined location.

Code Snippet

import pyautogui
img = pyautogui.screenshot( )
img.save(r"D:\pythonscreenshot.jpg")

Code Snippet


Output image stored in the desired location

Screenshot was taken by the above program (Output)


Comments

Popular posts from this blog

How to Create A Voice Recorder Using Python

  A voice recorder records a sound or a voice of a person and converts it into an audio file. The file can be stored in different audio formats like MP3 format, Waveform Audio File (WAV), Advanced Audio Coding (AAC), Audio Interchange File Format (AIFF), etc. and later it can be transferred to other devices. Any device that is capable of recording a sound or a voice is said to be a voice recorder by default. In this article, you will learn to build a voice recorder with a few lines of code in Python. Where do We Use Voice Recorder? Some of the applications of a voice recorder are listed below: Interviews Handling Meetings Lectures/Seminars in Educational Institutions Audiobooks for kids Learning foreign languages Translating a word to another language Google search by voice A quick voice message to a person instead of typing Podcasts Voice assistants like Google assistants, Alexa, Siri, etc., Essentials The first and foremost thing needed to build a voice recorder using Python is t...

Easy Way To Build An Audiobook Using Python

  An audiobook is nothing but a book that was recorded in an audio format. It can also be stated as a book that is being read aloud. Audiobook helps in improvising one's vocabulary, comprehension and pronunciation of words. Hope some of you guys are bookaholic but lazy to read it on your own. So it's time to create your own audiobook by using few codes of Python. This will definitely make you enjoy audiobooks without any subscription fee, that was imposed on platforms like Audible, Scribd, etc. Prerequisite Python has a huge number of modules that contains reusable code which performs desired functions when invoked. It must be installed into your system before using its functionalities. So here we going to grab just 2 modules from a bunch of python modules prevailing around. Install PyPDF2 module, using                     pip install PyPDF2 Install pyttsx3 module, using             ...

Translating text from one language to another using python

The definition of a translator is a  person who helps people who speak different languages to communicate or who take something  (such as a speech or a book) in one language and who puts it into a different language for people to understand.   This functionality can be achieved using the  googletrans module in python. googletrans module     The googletrans module is one of the fast and flexible libraries in python that implements Google Translate API,  to detect the language of text automatically and to convert text from one language to another. By default, all the languages are translated to English. Installation      This module can be installed by using,                pip install googletrans     googletrans can be used after installation and importation of the module               import googletrans or       ...