How to Take and Save a Screenshot in Python



Python


In this article, we show how to take a screenshot with Python.

Python has a module that you can install called the pyautogui module.

So, to install it, open up your command prompt of your computer and specify the path to the Scripts folder in the Python package. To install the pyautogui module, type in, pip install pyautogui.

Once this module has been successfully installed, then you're ready to go.

The code to take a screenshot in Python is shown below.



So, first, we must import the pyautogui module.

We then create a variable named pic that is set equal to pyautogui.screenshot(). This takes a screenshot of your current screen and saves it into the pic variable. So, the pic variable now contains the Image object of the screenshot.

If you want to see this screenshot picture taken (which you probably want to see), then you just take the variable and save it. This will save it to whatever current directory you are in.

If you want to save it to your desktop, then you either have to specify the full path to your desktop or you can import the os module and use the chdir() function to change to the the Desktop or whatever folder you want it saved to (such as the Pictures folder).

So, this is just a quick script to show how you can take and save a screenshot in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...