Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take screenshot in Python -- Cross Platform

I need to take a screenshot and send it via post to a web service. I think for the post part i will use liburl.

Can this be accomplished completely cross platform and without having the need for the final user to install additional libraries/software?

like image 498
Carlos Barbosa Avatar asked Dec 27 '11 12:12

Carlos Barbosa


People also ask

Can Python get the screen shot of a specific window?

You can use pywin32 and PIL to grab the image of a certain window (How to Get a Window or Fullscreen Screenshot in Python 3k? (without PIL)) You can use imagemagik to grab screenshots of a single window (https://www.imagemagick.org/discourse-server/viewtopic.php?t=24702)

How do I take a screenshot using Python selenium?

Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file.

What is MSS in Python?

What Is MSS? MSS is an ultra-fast cross-platform multiple screenshots module in pure python using ctypes. It supports Python 3.5 and above and is very basic and limited for what it does.


2 Answers

There is not anything in the standard library that can do this for you. Theoretically, you might do it yourself by making os-dependent system calls with ctypes but that seems like a lot of unnecessary work to me. Here is a working script to make a screenshot using wxPython:

import wx

app = wx.App(False)

s = wx.ScreenDC()
w, h = s.Size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 0, 0)
m.SelectObject(wx.NullBitmap)
b.SaveFile("screenshot.png", wx.BITMAP_TYPE_PNG)
like image 87
Toni Ruža Avatar answered Sep 29 '22 23:09

Toni Ruža


You could also use PyQt5 for this:

import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)
screen = QGuiApplication.primaryScreen()
desktopPixmap = screen.grabWindow(0)
desktopPixmap.save('screendump.png')
like image 40
Windel Avatar answered Sep 29 '22 23:09

Windel