[Python] Take a screenshot

environment

・ Mac Catalina -Python 3.8.1

Settings on Mac

If it is left as it is, the desktop will be taken even if the screenshot is taken, so Allows screen recording.

"System Preferences" → "Security & Privacy"

システム環境設定.jpg

Check the terminal in "Screen recording".

画面収録.jpg

Take a screenshot

Install pyautogui.

pip install pyautogui

You can take a full screen screenshot with pyautogui.screenshot ().

import pyautogui

screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')

全体.jpg

When specifying a range

By using region = (position from the left, position from the top, width, height), You can take a screenshot of a specific range.

import pyautogui

screenshot = pyautogui.screenshot(region = (100, 200, 1500, 875))
screenshot.save('screenshot.png')

範囲指定.jpg

When saving as JPEG

By converting to RGB, you can save as JPEG.

screenshot = screenshot.convert('RGB')

reference

Convert a large png file to a jpg file pyautogui

Recommended Posts

[Python] Take a screenshot
Take a screenshot in Python
[Python] [Windows] Take a screen capture in Python
A * algorithm (Python edition)
Create a Python module
A python lambda expression ...
Daemonize a Python process
ScreenShot with Selenium (Python)
Create a Python environment
Python3> round (a --b, 7)
Take a screenshot of the LCD with Python-LEGO Mindstorms
Create a function in Python
Create a dictionary in Python
[Python3] Take a screenshot of a web page on the server and crop it further
AtCoder ABC 178 Python (A ~ E)
A road to intermediate Python
[Python] Use a string sequence
AtCoder ABC 176 Python (A ~ E)
Python list is not a list
A memorandum about correlation [Python]
Make a bookmarklet in Python
Building a Python virtual environment
Create a python numpy array
Make a fortune with Python
I made a python text
A memorandum about Python mock
AtCoder ABC 182 Python (A ~ D)
Build a Python environment offline
Draw a heart in Python
Create a directory with python
Building a Python virtual environment
[Python] Dynamic programming TDPC A
What is a python map?
A note about [python] __debug__
Quickly take a query string with API Gateway-> Lambda (Python)
Take a look at the Python built-in exception tree structure
Building a Python environment on Mac
Python a + = b and a = a + b are different
A record of patching a python package
[Python] What is a zip function?
[Python] What is a with statement?
Write a binary search in Python
Use pymol as a python library
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
[python] Manage functions in a list
Touch a Python object from Elixir
Hit a command in Python (Windows)
I made a Line-bot using Python!
Python
Take a look at Django's template.
ABC127 A, B, C Explanation (python)
Create a python GUI using tkinter
Create a DI Container in Python
Let's make a GUI with python.
Building a Python environment on Ubuntu
Python: A Note About Classes 1 "Abstract"
Drawing a silverstone curve using python
[Python] A memorandum of beautiful soup4
Solve ABC166 A ~ D with Python