I investigated how to automate screen captures on Windows. (Use Python this time)
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
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.
The ImageGrab Module pyscreenshot 0.4.2 : Python Package Index
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
Another way to do a screen capture of any application is to use pywinauto
.
Install pywin auto
$ pip install pywinauto
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.
Pywinauto - pywinauto What is pywinauto — pywinauto 0.6.0 documentation Click the GUI menu in pywinauto: Office worker, computer and me
Recommended Posts