I am creating a Beta Testers reporting module so they can send in thier comments on my software, but I would like to have the option to include a screenshot with the report. How do I take a screenshot of the screen with Python on Windows? I have found several examples on Linux, but haven't had much luck on Windows.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
For pyautogui users:
Worth noting that ImageGrab only works on MSWindows.
For cross platform compatibility, a person may be best off with using the wxPython library. http://wiki.wxpython.org/WorkingWithImages#A_Flexible_Screen_Capture_App
Another approach that is really fast is the MSS module. It is different from other solutions in the way that it uses only the
ctypes
standard module, so it does not require big dependencies. It is OS independant and its use is made easy:And just find the
screenshot.png
file containing the screen shot of the first monitor. There are a lot of possibile customizations, you can play withScreenShot
objects and OpenCV/Numpy/PIL/etc..A simple way to take a screenshot is through Pygame.
Where 'Surface' is the surface you are taking a screenshot of, and 'filename' is the file path, name, and type where you save thew image.
You can export as BMP, TGA, PNG, or JPEG. As of Pygame 1.8, PNG, and JPEG also work.
If no file extension is specified it will default to a .TGA file.
You can even use the 'os' library for saving to specific file directories.
An example:
This saves anything on the 'surface' Surface to the user's desktop as pic.png
You can use the ImageGrab module. ImageGrab works on Windows and macOS, and you need PIL (Pillow) to use it. Here is a little example:
If you want to snap particular running Windows app you’ll have to acquire a handle by looping over all open windows in your system.
It’s easier if you can open this app from Python script. Then you can convert process pid into window handle.
Another challenge is to snap the app that runs in particular monitor. I have 3 monitor system and I had to figure out how to snap display 2 and 3.
This example will take multiple application snapshots and save them into JPEG files.
Details are here