take screenshots in python

There are several ways to take screenshots in Python. Here are two of the most popular libraries to achieve this:

  1. Using the Pillow Library:
main.py
from PIL import ImageGrab

# Grab the current screen image and save it to a file
im = ImageGrab.grab()
im.save('screenshot.png')
129 chars
6 lines
  1. Using the PyAutoGUI Library:
main.py
import pyautogui

# Take a screenshot of the entire screen and save it to a file
pyautogui.screenshot('screenshot.png')
120 chars
5 lines

Note that both of these libraries require installation, which can be done using pip package manager. Additionally, the tkinter library can be used to display the captured screenshot in a GUI application.

gistlibby LogSnag