Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screenshot on Windows with Python?

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.

like image 479
Zac Brown Avatar asked May 17 '10 06:05

Zac Brown


People also ask

Can we take screenshot in Python?

The pyautogui module makes use of the screenshot function which is responsible for taking the screenshot of the whole computer screen. And then the save function is used to save the screenshot captured to our device.

How do I take a screenshot in Pycharm?

Select a code and press a hot-key (Ctrl+Alt+Shift+A by default) to copy it as the image (make a screenshot).

How do I take a screenshot using selenium Python?

Take the first screenshot using Selenium WebDriver and function save_screenshot() Take a screenshot using Python selenium-screenshot – Using Screenshot_Clipping from the Screenshot module, and the full_Screenshot() function.


2 Answers

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:

from mss import mss  with mss() as sct:     sct.shot() 

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 with ScreenShot objects and OpenCV/Numpy/PIL/etc..

like image 62
Tiger-222 Avatar answered Sep 22 '22 13:09

Tiger-222


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

import wx app = wx.App()  # Need to create an App instance before doing anything screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.Bitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) del mem  # Release bitmap bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG) 
like image 31
Sepero Avatar answered Sep 19 '22 13:09

Sepero