[Python] [Windows] Take a screen capture in Python

I investigated how to automate screen captures on Windows. (Use Python this time)

Use PIL

Advance preparation

PIL(Pillow)Installation of


$ pip install Pillow

There was also a pyscreenshot, but for Windows, the implementation is the same because PIL is used internally after all.

Install pyscreenshot


$ pip install pyscreenshot

Implementation example

Screen capture example using PIL


from PIL import ImageGrab

# full screen
ImageGrab.grab().save("PIL_capture.png ")

#Clipping within the specified area
ImageGrab.grab(bbox=(100, 100, 200, 200)).save("PIL_capture_clip.png ")

With the above implementation, you can do full screen screen capture, clipping and output to pig. Since PIL is multifunctional, it also supports image processing and saving in another format (jpeg, etc.), but I think that you should check it separately.

However, there were two problems with this implementation.

reference

The ImageGrab Module pyscreenshot 0.4.2 : Python Package Index

Use Selenium

It is a problem that PIL application cannot be specified, but if you want to specify a Web browser, there seems to be a way to use Selenium. (Unverified because it did not fit my use case)

ScreenShot with Selenium (Python version) --Qiita Operate Firefox with Selenium from python and save screen captures --Qiita How to take partial screenshot with Selenium WebDriver in python? - Stack Overflow

Use pywin auto

Another way to do a screen capture of any application is to use pywinauto.

Advance preparation

Install pywin auto


$ pip install pywinauto

Example of use

Example of using pywinauto


# coding: UTF-8

from pywinauto import application
from time import sleep

app = application.Application().start("notepad.exe")

sleep(1)
app[u'Untitled.*Notepad'].CaptureAsImage().save('window.png')

You now have a screenshot of any application (Notepad in the example). As a caveat, CaptureAsImage does not directly fetch the drawing buffer, it seems that it only clips the area where the window exists, and in the above sample it is before starting up unless sleep is inserted. The capture has run.

reference

Pywinauto - pywinauto What is pywinauto — pywinauto 0.6.0 documentation Click the GUI menu in pywinauto: Office worker, computer and me

Recommended Posts

[Python] [Windows] Take a screen capture in Python
Take a screenshot in Python
Hit a command in Python (Windows)
[Python] Take a screenshot
Create a function in Python
Create a dictionary in Python
Until drawing a 3D graph in Python on windows10
Make a bookmarklet in Python
Draw a heart in Python
Python install in 2 lines @Windows
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[Python] Show multiple windows in Tkinter
[python] Manage functions in a list
Create a DI Container in Python
Draw a scatterplot matrix in python
Get web screen capture with python
Write A * (A-star) algorithm in Python
Create a binary file in Python
Windows10: Install MeCab library in python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Create a Python environment for professionals in VS Code on Windows
Take a look at the built-in exception tree structure in Python 3.8.2
Generate a first class collection in Python
Mouse operation using Windows API in Python
Use print in a Python2 lambda expression
A simple HTTP client implemented in Python
Do a non-recursive Euler Tour in Python
Using venv in Windows + Docker environment [Python]
I made a payroll program in Python!
Precautions when pickling a function in python
Display a list of alphabets in Python 3
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Building a Python 3.6 environment with Windows + PowerShell
[Python] [Windows] Serial communication in Python using DLL
Write a short property definition in Python
Draw a heart in Python Part 2 (SymPy)
Run the Python interpreter in a script
Creating a python virtual environment on Windows
How to get a stacktrace in python
Write a Caesar cipher program in Python
Hash in Perl is a dictionary in Python
Scraping a website using JavaScript in Python
Write a simple greedy algorithm in Python
Launch a Flask app in Python Anywhere
Get a token for conoha in python
Solve ABC165 A, B, D in Python
[GPS] Create a kml file in Python