Take a screenshot via a Python script on Linux

后端 未结 15 1018
暖寄归人
暖寄归人 2020-11-22 11:50

I want to take a screenshot via a python script and unobtrusively save it.

I\'m only interested in the Linux solution, and should support any X based environment.

相关标签:
15条回答
  • 2020-11-22 12:41

    Cross platform solution using wxPython:

    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)
    
    0 讨论(0)
  • 2020-11-22 12:42

    It's an old question. I would like to answer it using new tools.

    Works with python 3 (should work with python 2, but I haven't test it) and PyQt5.

    Minimal working example. Copy it to the python shell and get the result.

    from PyQt5.QtWidgets import QApplication
    app = QApplication([])
    screen = app.primaryScreen()
    screenshot = screen.grabWindow(QApplication.desktop().winId())
    screenshot.save('/tmp/screenshot.png')
    
    0 讨论(0)
  • 2020-11-22 12:46

    From this thread:

     import os
     os.system("import -window root temp.png")
    
    0 讨论(0)
提交回复
热议问题