Get screenshot on Windows with Python?

162,986

Solution 1

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..

Solution 2

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)

Solution 3

This can be done with PIL. First, install it, then you can take a full screenshot like this:

import PIL.ImageGrab

im = PIL.ImageGrab.grab()
im.show()

Solution 4

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:

from PIL import ImageGrab
snapshot = ImageGrab.grab()
save_path = "C:\\Users\\YourUser\\Desktop\\MySnapshot.jpg"
snapshot.save(save_path)

Solution 5

For pyautogui users:

import pyautogui
screenshot = pyautogui.screenshot()
Share:
162,986
Zac Brown
Author by

Zac Brown

Computer geek, entrepreneur, soccer freak, Founder & CEO TeraTech Mobile, avid cyclist. That is all.

Updated on July 08, 2022

Comments

  • Zac Brown
    Zac Brown almost 2 years

    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.

  • tommy.carstensen
    tommy.carstensen over 10 years
    AttributeError: 'module' object has no attribute 'App'
  • cSn
    cSn over 10 years
    app = wx.App() otherwise you might get: wx._core.PyNoAppError: The wx.App object must be created first! Also, download wxPython from here: wxpython.org/download.php For some reason it's not the same "wx" if you just do "pip install wx"
  • olekb
    olekb over 6 years
    CLI windows coming out empty
  • tal
    tal over 6 years
  • Adrian Keister
    Adrian Keister over 6 years
    Two questions: 1. Is pygame platform-independent? 2. Can you get a multiple-monitor screenshot?
  • Adrian Keister
    Adrian Keister over 6 years
    @tal: Your link is outdated. Could you please update?
  • Adrian Keister
    Adrian Keister over 6 years
    @Tiger-222: How would you get multiple monitors?
  • Adrian Keister
    Adrian Keister over 6 years
    @Sepero: How would you get multiple monitors?
  • Tiger-222
    Tiger-222 over 6 years
    @AdrianKeister: The good link is python-mss.readthedocs.io/en/latest/examples.html ; you will find answers :)
  • Adrian Keister
    Adrian Keister over 6 years
    @Tiger-222: Excellent! One thing more: when I did one monitor with your code, I got a very nicely small 63kB file. In the examples link, when I did a single screenshot for all three monitors, I get a whopping 1.2 MB file. Any way to get the full screenshot down to compare in size with the single monitor?
  • Adrian Keister
    Adrian Keister over 6 years
    Never mind. I found out that the png filesize is a lot more dependent on what's on the screen than anything else. Thanks again!
  • CPSuperstore
    CPSuperstore over 6 years
    As far as I know, Pygame is platform is independent. This code will only screenshot the pygame screen, and only the pygame screen. If your screen spans across 2 monitors, i would think that it would be able to (though I am unable to test this). Hope this helps!
  • Yan Khonski
    Yan Khonski almost 6 years
    And how do I write the image into pgn file?
  • Al Sweigart
    Al Sweigart almost 6 years
    pyautogui.screenshot('filename.png')
  • V15I0N
    V15I0N about 5 years
    @ChristianAdam: your must pip install -U wxPython
  • ali reza
    ali reza almost 5 years
    the result size of image with PIL is better than above module: from PIL import ImageGrab snapshot = ImageGrab.grab() save_path = r"E:\havaee\mypic.jpg" snapshot.save(save_path)
  • GNUton
    GNUton almost 5 years
    PIL.ImageGrab is only for Windows and OS X. Linux alternative to ImageGrab is pyscreenshot
  • Hanz
    Hanz almost 4 years
    wx.EmptyBitmap is deprecated: wxPyDeprecationWarning: Call to deprecated item EmptyBitmap. Use :class :wx.Bitmap instead use wx.Bitmap
  • Brent Vollebregt
    Brent Vollebregt over 3 years
  • tsbertalan
    tsbertalan over 3 years
    Note that you actually need to assign the result of wx.App() as cSN writes, otherwise it will get GC'd with unpredictable results.
  • Barney Szabolcs
    Barney Szabolcs over 3 years
    MSS is a lot faster than the others because it does not save the screen into a temp file but relies on a system CoreGraphics module (on OS X at least). So, you can probably either avoid saving pngs altogether or you may want to save the screenshots as jpeg.
  • Barney Szabolcs
    Barney Szabolcs over 3 years
    I would rather go by MSS, this one works by running a terminal command and saving the whole screen into a png.