Take a screenshot via a Python script on Linux

后端 未结 15 1165
暖寄归人
暖寄归人 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:40

    This works without having to use scrot or ImageMagick.

    import gtk.gdk
    
    w = gtk.gdk.get_default_root_window()
    sz = w.get_size()
    print "The size of the window is %d x %d" % sz
    pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
    pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
    if (pb != None):
        pb.save("screenshot.png","png")
        print "Screenshot saved to screenshot.png."
    else:
        print "Unable to get the screenshot."
    

    Borrowed from http://ubuntuforums.org/showpost.php?p=2681009&postcount=5

提交回复
热议问题