[Python] linux下python抓屏小程序 →→→→→进入此内容的聊天室

来自 , 2020-05-31, 写在 Python, 查看 106 次.
URL http://www.code666.cn/view/16c0d78e
  1. #!/usr/bin/python
  2. '''by zevolo, 2012.12.20
  3. '''
  4.  
  5. import gtk.gdk
  6. import gtk
  7. import glib
  8.  
  9. class MyRect():
  10.     def __init__(self, x = 0, y = 0, w = 0, h = 0):
  11.         self.x = x
  12.         self.y = y
  13.         self.w = w
  14.         self.h = h
  15.     def __init__(self, x, y):
  16.         self.x = min(int(x.x), int(y.x))
  17.         self.y = min(int(x.y), int(y.y))
  18.         self.w = abs(int(y.x - x.x))
  19.         self.h = abs(int(y.y - x.y))
  20.  
  21. class MyPair():
  22.     def __init__(self, x = 0, y = 0):
  23.         self.x = x
  24.         self.y = y
  25.  
  26. class MyPoint(MyPair):
  27.     def __init__(self, x = 0, y = 0):
  28.         MyPair.__init__(self, x, y)
  29.  
  30. class MySize(MyPair):
  31.     def __init__(self, w = 0, h = 0):
  32.         MyPair.__init__(self, x, y)
  33.  
  34.  
  35. class MyCapture():
  36.     (event_enter, event_leave) = (0, 1)
  37.     def __init__(self):
  38.         pass
  39.  
  40.     def capture(self):
  41.         pass
  42.  
  43.     def handleEvent(self, event):
  44.         if event == event_enter:
  45.             enterSnap()
  46.         elif event == event_leave:
  47.             leaveSnap()
  48.  
  49.     def enterSnap(self):
  50.         pass
  51.  
  52.     def leaveSnap(self):
  53.         pass
  54.  
  55.     def snap(self, window = None, rect = None, name = None):
  56.         pass
  57.  
  58.  
  59. class MyCaptureGtk(MyCapture):
  60.     def __init__(self):
  61.         MyCapture.__init__(self)
  62.         self.window = gtk.Window()
  63.         self.window.set_default_size(1,1)
  64.         self.window.connect("button-press-event", self.button_press_cb)
  65.         self.first = None
  66.         self.second = None
  67.         self.window.show()
  68.         #self.window.set_events(gtk.gdk.BUTTON_PRESS_MASK)
  69.  
  70.     def getWindow(self):
  71.         return self.window
  72.  
  73.     def button_press_cb(self, widget, event):
  74.         #print "type is %d" % event.type
  75.         if event.type == gtk.gdk.BUTTON_PRESS:
  76.             if event.button == 1: #left button
  77.                 print "(%d, %d), (%d, %d), button is %d" % (event.x_root, event.y_root, event.x, event.y, event.button)
  78.                 if not self.first:
  79.                     self.first = MyPoint(event.x_root, event.y_root)
  80.                 else:
  81.                     self.second = MyPoint(event.x_root, event.y_root)
  82.                     self.snap(None, MyRect(self.first, self.second))
  83.                     self.first = None
  84.             elif event.button == 3: #right button
  85.                 self.uncapture()
  86.             else:
  87.                 pass
  88.  
  89.     def uncapture(self):
  90.         if self.first:
  91.             print "cancel"
  92.             self.first = None
  93.         else:
  94.             print "exit now"
  95.             gtk.gdk.pointer_ungrab()
  96.             gtk.mainquit()
  97.  
  98.     def capture(self, time = 0L):
  99.         cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default(), gtk.gdk.CROSSHAIR)
  100.         ret = gtk.gdk.pointer_grab(self.window.window, True,
  101.                     gtk.gdk.BUTTON_PRESS_MASK,
  102.                     None, cursor, time)
  103.         if ret == gtk.gdk.GRAB_SUCCESS:
  104.             print "left button start, end, right button cancel/exit"
  105.         else:
  106.             print "failed to capture %d, (viewable %d),(frozen %d), (already %d)" \
  107.               % (ret, gtk.gdk.GRAB_NOT_VIEWABLE, gtk.gdk.GRAB_FROZEN, gtk.gdk.GRAB_ALREADY_GRABBED)
  108.  
  109.     def snap(self, window = None, rect = None, name = None):
  110.         w = window
  111.         if not window:
  112.             #w = gtk.gdk.get_default_root_window()
  113.             d = gtk.gdk.display_get_default()
  114.             w = d.get_default_screen().get_root_window()
  115.  
  116.         r = rect
  117.         if not r:
  118.             sz = w.get_size()
  119.             r = MyRect(0, 0, sz[0], sz[1])
  120.         print "The size of the window is (%d, %d, %d, %d)" % (r.x, r.y, r.w, r.h)
  121.  
  122.         n = name
  123.         if not n:
  124.             n = "screenshot.png"
  125.  
  126.         buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8, r.w, r.h)
  127.         buf = buf.get_from_drawable(w,w.get_colormap(), r.x, r.y, 0, 0, r.w, r.h)
  128.         if (buf != None):
  129.             buf.save(n, "png")
  130.             print "Screenshot saved to %s." % n
  131.         else:
  132.             print "Unable to get the screenshot."
  133.  
  134. def timeout(data):
  135.     #print "timeout"
  136.     data.capture()
  137.  
  138. if __name__ == '__main__':
  139.     cap = MyCaptureGtk()
  140.     w = cap.getWindow()
  141.     w.show()
  142.     glib.timeout_add_seconds(1, timeout, cap)
  143. #cap.snap()
  144.     gtk.main()
  145.  
  146. #//python/5992

回复 "linux下python抓屏小程序"

这儿你可以回复上面这条便签

captcha