Python grasping screen implementation method under Linux

Source: Internet
Author: User
Tags abs gtk min

The example of this article describes the Linux Python grab screen implementation method. Share to everyone for your reference. The specific implementation code is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 #!/usr/bin/python ' by Zevolo, 2012.12.20 ' ' Import gtk.gdk import GTK Import glib Class Myrect (): Def __init__ (self, X = 0, y = 0, W = 0, h = 0): self.x = x self.y = y SELF.W = w self.h = h def __init__ (self, x, y): self.x = min (int (x.x), I NT (y.x)) self.y = min (int (x.y), int (y.y)) SELF.W = ABS (int (y.x-x.x)) self.h = ABS (int (Y.Y-X.Y)) class Mypair (): Def __ Init__ (self, x = 0, y = 0): self.x = x self.y = y class MyPoint (Mypair): Def __init__ (self, x = 0, y = 0): mypair.__init__ (self, x, Y) class Mysize (Mypair): Def __init__ (self, w = 0, h = 0): mypair.__init__ (self, x, Y) class Mycapture (): (Event _enter, Event_leave) = (0, 1) def __init__ (self): Pass Def Capture (self): Pass def handleevent (self, event): If event = E Vent_enter:entersnap () elif event = = Event_leave:leavesnap () def entersnap (self): Pass def Leavesnap (self): Pass def SNA P (self, window = none, rect = none, name = None): Pass class MYCAPTUREGTK (mycapture): def __init__ (self): Mycapture.__init __ (self) Self.window = gtk. Window () self.window.set_default_size (1,1) self.window.connect ("Button-press-event", SELF.BUTTON_PRESS_CB) Self.first = None Self.second = None self.window.show () #self. Window.set_events (Gtk.gdk.BUTTON_PRESS_MASK) def GetWindow (self): return Self.window def button_press_cb (self, Widget, event): #print ' type is%d '% event.type if event.ty PE = = Gtk.gdk.BUTTON_PRESS:if Event.button = = 1: #left button print (%d,%d), (%d,%d), button is%d "%" (Event.x_root, E Vent.y_root, Event.x, Event.y, Event.button) if not Self.first:self.first = MyPoint (Event.x_root, Event.y_root) Else:sel F.second = MyPoint (Event.x_root, Event.y_root) Self.snap (None, Myrect (Self.first, self.second)) Self.first = None elif ev Ent.button = 3: #right button Self.uncapture () Else:pass def uncapture (self): if Self.first:print "Cancel" Self.first = None else:print ' Exit Now ' Gtk.gdk.pointer_ungrab () gtk.mainquit () def capture (self, time = 0L): cursor = Gtk.gdk.Cursor (Gtk.gdk.display_get_default (), Gtk.gdk.CROSSHAIR) ret = Gtk.gdk.pointer_grab (Self.window.window, True, Gtk.gdk.BUTTON_PRESS_MASK, None, cursor, time) if ret = = GTK.GDK.G Rab_success:print "left button start, end, right button Cancel/exit" Else:print "failed to capture%d, (viewable%d), (FR Ozen%d), (already%d) "% (ret, Gtk.gdk.GRAB_NOT_VIEWABLE, Gtk.gdk.GRAB_FROZEN, Gtk.gdk.GRAB_ALREADY_GRABBED) def snap ( Self, window = none, rect = none, name = none): w = window if not window: #w = Gtk.gdk.get_default_root_window () d = gtk.g Dk.display_get_default () W = D.get_default_screen (). Get_root_window () R = Rect if not R:sz = W.get_size () R = myrect (0, 0 , Sz[0], sz[1]) print "The size of the" window is (%d,%d,%d,%d)% (r.x, r.y, R.W, r.h) n = name if not n:n = "Screensh Ot.png "buf = Gtk.gdk.Pixbuf (gtk.gdk.colorspace_rgb,false,8, R.W, r.h) buf = buf.get_from_drawable (W,w.get_colormap (), R.x, r.y, 0, 0, R.W, r.h) if (buf!= None): Buf.save (N, "PNG") print screenshot saved to%s. "% n else:print" unable to Get the screenshot. "Def TimeoUT (data): #print "Timeout" data.capture () if __name__ = = ' __main__ ': Cap = MYCAPTUREGTK () w = Cap.getwindow () w.show () Gli B.timeout_add_seconds (1, timeout, cap) #cap. Snap () Gtk.main ()

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.