Home  >  Article  >  Backend Development  >  How to take screenshot in python

How to take screenshot in python

(*-*)浩
(*-*)浩Original
2019-06-27 14:03:084258browse

Today I will talk about how to use Python to take screenshots.

How easy is it to take screenshots in Python, which is famous for its simplicity?

How to take screenshot in python

In fact, it only takes two lines of code to do it: (recommended learning: Python video tutorial)

from PIL import ImageGrab
ImageGrab.grab().show()
## What is #PIL? The full name of PIL is Pillow, which means "pillow" in English. It is an excellent image processing library on Python.

What should be noted here is that PIL is a package, not a module, so when we import, we always import from PIL import module name.

from PIL import ImageGrab Import the ImageGrab module in the PIL package

ImageGrab.grab().show()

The ImageGrab.grab() method returns an Image object and then calls The object's show method

grab method only accepts one parameter, which sets the screenshot area. The parameter must be a tuple type

The parameter tuple format is (area start x-axis coordinate, Area start y-axis coordinate, area end x-axis coordinate, area end y-axis coordinate)

Or understand it this way (upper left corner x, upper left corner y, lower right corner x, lower right corner y)

When the grab method has no parameters, the full screen will be captured.

The function of the show method is to save the Image object as a picture and then open it. The saving location is the temporary folder of the current user.

For more Python related technical articles, please visit the

Python Tutorial column to learn!

The above is the detailed content of How to take screenshot in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn