Implementation of python screen capture in linux

Source: Internet
Author: User

Implementation of python screen capture in linux

This article describes how to implement python screen capture in linux. Share it with you 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

43

44

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

84

85

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

119

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), int (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 = event_enter:

EnterSnap ()

Elif event = event_leave:

LeaveSnap ()

Def enterSnap (self ):

Pass

Def leaveSnap (self ):

Pass

Def snap (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. type = gtk. gdk. BUTTON_PRESS:

If event. button = 1: # left button

Print "(% d, % d), (% d, % d), button is % d" % (event. x_root, event. y_root, event. x, event. y, event. button)

If not self. first:

Self. first = MyPoint (event. x_root, event. y_root)

Else:

Self. second = MyPoint (event. x_root, event. y_root)

Self. snap (None, MyRect (self. first, self. second ))

Self. first = None

Elif event. 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. GRAB_SUCCESS:

Print "left button start, end, right button cancel/exit"

Else:

Print "failed to capture % d, (viewable % d), (frozen % 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. gdk. 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)" % (r. x, r. y, r. w, r. h)

N = name

If not n:

N = "screenshot.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 ()

Glib. timeout_add_seconds (1, timeout, cap)

# Cap. snap ()

Gtk. main ()

I hope this article will help you with 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.