SoFunction
Updated on 2024-12-17

Example Summary of Five Ways to Take a Python Screenshot

Small master I summarized, look at the official for reference only. Specific running time, depending on the computer, the complexity of the program, the size of the screenshot, originally for the four methods, and then found a kind. Add
Factors such as operational proficiency.

Method I:

It's fast, intercepting a single sheet in about zero seconds. But the program is long.

As pictured:

The red box is to get the full screen size of the current monitor device. Or you can do without it, you can directly assign values to w, h, specify the width and height. Above the red box is to get the device ready condition.
The first red line, is the left margin and the top margin fill background color black, default (0,0) is good, you can also try it yourself. Here do not add a picture.

The second red line is the specified image width and height (>0). The third red line, the coordinates of the upper-left corner of the screenshot taken, if less than 0, the transgression is filled with black.

Method II:

The speed is average, about 1 second to intercept a single sheet, and the program is simple and easy to understand.grab()If no parameter is added, it is full screen. The four parameters are
x1,y1,x2,y2. i.e., upper left and lower right coordinates.

Method Three:

Probably 1 to 2 seconds as well. This library, however, has to be installed separately.

screenshot() without the region parameter is fullscreen. The four parameters are
x,y,w,h. i.e., top-left corner coordinates and image width Height.

Method four:

Screenshots can be taken with the crawler library selenium.

The main program should be written as it is, screenshots are used:

browser.save_screenshot(filename), the size parameter is something you can try yourself. Maybe I'll add it later

If you define a function, pay attention to the way you write the formal parameter and the passing parameter.

Method V:

Use aotupy to take a screenshot as shown:

capture_screen() function, the parentheses if empty, is the full screenshot. If you want to specify the range, you have to write it this way, because it requires an array of length 2 when the parameter, the function should be with parentheses, the array should be with parentheses, the length of 2, two groups of numbers, so, pay attention to the parentheses match. The first, is the starting position, the second parentheses, is from the starting point of the long read and width, so to calculate.

Supplementary: Python Specified Area Screenshot

Let's start with the code.

from pyautogui import screenshot
import time
from PIL import ImageGrab
# Grab Screenshot of Screen
def grab_screenshot():
    shot = screenshot()
    ('my_screenshot.png')
# Grab Screenshot of Specific Area
def grab_screenshot_area():
    area = (0, 0, 500, 500)   # Here's the area to set the screenshot range
    shot = (area)
    ('my_screenshot_area.png')
# Grab Screenshot with Delay
def grab_screenshot_delay():
    (5)
    shot = screenshot()
    ('my_screenshot_delay.png')

Using this script, you can take screenshots directly or capture screenshots of specific areas.

It can be loaded in some robocall scripts to grab the spike product, take a screenshot, and then move on to the next one.

summarize

To this article on the Python screenshot of the five methods summarized in the article is introduced to this, more related Python screenshot method content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!