Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Python: Taking a screenshot

  1. #1
    Join Date
    Dec 2005
    Beans
    113
    Distro
    Ubuntu 7.04 Feisty Fawn

    Python: Taking a screenshot

    I want to take a screenshot using Python. I can import and use any module necessary, and the solution can be platform-dependent. This seems like it should be simple using pygtk or something, but my Google-fu fails me. Any ideas?

  2. #2
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: Python: Taking a screenshot

    you could install a program like scrott, then just call that program from python
    Edward A Robinson -- www.earobinson.org

  3. #3
    Join Date
    Dec 2005
    Beans
    113
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Taking a screenshot

    Aha! Yes, immediately after posting this it occurred to me I could just call ImageMagick (or scrot).

    Code:
    import os
    os.system("import -window root temp.png")
    Perfect! Thanks.

  4. #4
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: Python: Taking a screenshot

    sweet. Glad things are working.
    Edward A Robinson -- www.earobinson.org

  5. #5
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Python: Taking a screenshot

    In case you ever want to do it without using system():
    Code:
    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."

  6. #6
    Join Date
    Dec 2005
    Beans
    113
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Taking a screenshot

    Now, that's nice, too! It's a good bit faster that invoking ImageMagick, as well. That's pretty much what I had originally thought I might be able to do, but hadn't been able to spelunk through the pygtk docs well enough to figure out. It's excellent to have two ways to do it.

    I greatly appreciate the help from both of you.

  7. #7
    Join Date
    Feb 2005
    Beans
    19

    Re: Python: Taking a screenshot

    Sorry to revive this thread from the dead, but I'm looking for the EXACT same thing, except that my program needs to be cross-platform. Obviously the system-based solution won't work, but what about the other one? Does the GTK module come with Python for Windows or does it require a separate installation? I would try this myself except that my laptop is currently down with its motherboard broken.

  8. #8
    Join Date
    Dec 2005
    Beans
    113
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Taking a screenshot

    Unfortunately, installing PyGTK on Windows requires four separate installations for the GTK+ stack bundle, PyCairo, PyGobject and PyGTK. It's a bit of a pain, although it does work. If you're not using GTK+ for a GUI, it may be easier just to use PIL to take the screenshot on Windows.

  9. #9
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python: Taking a screenshot

    Quote Originally Posted by musicinmybrain View Post
    Unfortunately, installing PyGTK on Windows requires four separate installations for the GTK+ stack bundle, PyCairo, PyGobject and PyGTK. It's a bit of a pain, although it does work. If you're not using GTK+ for a GUI, it may be easier just to use PIL to take the screenshot on Windows.
    They would still have to install image magick otherwise.

  10. #10
    Join Date
    Dec 2005
    Beans
    113
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Taking a screenshot

    Quote Originally Posted by days_of_ruin View Post
    They would still have to install image magick otherwise.
    In which case?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •