Get screenshot on Windows with Python?

Posted on

Question :

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.

Answer #1:

Worth noting that ImageGrab only works on MSWindows.

For cross platform compatibility, a person may be best off with using the wxPython library.

import wx
wx.App()  # Need to create an App instance before doing anything
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(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)
Answered By: Sepero

Answer #2:

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:

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

Answered By: Tiger-222

Answer #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()
Answered By: Vivek Gupta

Answer #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"
Answered By: Somebody

Answer #5:

For pyautogui users:

import pyautogui
screenshot = pyautogui.screenshot()
Answered By: Zaid E.

Answer #6:

A simple way to take a screenshot is through Pygame., filename)

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:

import os
import pygame
surface = pygame.display.set_mode((100, 100), 0, 32)
surface.fill((255, 255, 255)), (0, 0, 0), (10, 10), 15, 0)
pygame.display.update(), os.path.expanduser("~/Desktop/pic.png"))

This saves anything on the ‘surface’ Surface to the user’s desktop as pic.png

Answered By: CPSuperstore

Answer #7:

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.

import wx

app=wx.App()  # Need to create an App instance before doing anything
displays = (wx.Display(i) for i in range(wx.Display.GetCount()))
sizes = [display.GetGeometry().GetSize() for display in displays]

for (i,s) in enumerate(sizes):
    print("Monitor{} size is {}".format(i,s))   
screen = wx.ScreenDC()
size = screen.GetSize()

print("Width = {}".format(size[0]))
print("Heigh = {}".format(size[1]))

x,y,w,h =putty_rect

bmp = wx.Bitmap(w,h)
mem = wx.MemoryDC(bmp)

for i in range(98):
    if 1:
        #1-st display:


        mem.Blit(-x,-y,w+x,h+y, screen, 0,0)

    if 0:
        #2-nd display:
        mem.Blit(0, 0, x,y, screen, width,0)

    if 0:
        #3-rd display:
        mem.Blit(0, 0, width, height, screen, width*2,0)

    bmp.SaveFile(os.path.join(home,"image_%s.jpg" % i), wx.BITMAP_TYPE_JPEG)    
    print (i)
del mem

Details are here

Answered By: Alex B

Answer #8:

import pyautogui

s = pyautogui.screenshot()'C:\Users\NAME\Pictures\s.png')
Answered By: tinus

Leave a Reply

Your email address will not be published. Required fields are marked *