Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / swig_python / drawing.py
1 #! /usr/bin/env python
2
3 print "OpenCV Python version of drawing"
4
5 # import the necessary things for OpenCV
6 from opencv import cv
7 from opencv import highgui
8
9 # for making random numbers
10 from random import Random
11
12 def random_color (random):
13     """
14     Return a random color
15     """
16     icolor = random.randint (0, 0xFFFFFF)
17     return cv.cvScalar (icolor & 0xff, (icolor >> 8) & 0xff, (icolor >> 16) & 0xff)
18
19 if __name__ == '__main__':
20
21     # some "constants"
22     width = 1000
23     height = 700
24     window_name = "Drawing Demo"
25     number = 100
26     delay = 5
27     line_type = cv.CV_AA  # change it to 8 to see non-antialiased graphics
28     
29     # create the source image
30     image = cv.cvCreateImage (cv.cvSize (width, height), 8, 3)
31
32     # create window and display the original picture in it
33     highgui.cvNamedWindow (window_name, 1)
34     cv.cvSetZero (image)
35     highgui.cvShowImage (window_name, image)
36
37     # create the random number
38     random = Random ()
39
40     # draw some lines
41     for i in range (number):
42         pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
43                           random.randrange (-height, 2 * height))
44         pt2 = cv.cvPoint (random.randrange (-width, 2 * width),
45                           random.randrange (-height, 2 * height))
46         cv.cvLine (image, pt1, pt2,
47                    random_color (random),
48                    random.randrange (0, 10),
49                    line_type, 0)
50         
51         highgui.cvShowImage (window_name, image)
52         highgui.cvWaitKey (delay)
53
54     # draw some rectangles
55     for i in range (number):
56         pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
57                           random.randrange (-height, 2 * height))
58         pt2 = cv.cvPoint (random.randrange (-width, 2 * width),
59                           random.randrange (-height, 2 * height))
60         cv.cvRectangle (image, pt1, pt2,
61                         random_color (random),
62                         random.randrange (-1, 9),
63                         line_type, 0)
64         
65         highgui.cvShowImage (window_name, image)
66         highgui.cvWaitKey (delay)
67
68     # draw some ellipes
69     for i in range (number):
70         pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
71                           random.randrange (-height, 2 * height))
72         sz = cv.cvSize (random.randrange (0, 200),
73                         random.randrange (0, 200))
74         angle = random.randrange (0, 1000) * 0.180
75         cv.cvEllipse (image, pt1, sz, angle, angle - 100, angle + 200,
76                         random_color (random),
77                         random.randrange (-1, 9),
78                         line_type, 0)
79         
80         highgui.cvShowImage (window_name, image)
81         highgui.cvWaitKey (delay)
82
83     # init the list of polylines
84     nb_polylines = 2
85     polylines_size = 3
86     pt = [0,] * nb_polylines
87     for a in range (nb_polylines):
88         pt [a] = [0,] * polylines_size
89
90     # draw some polylines
91     for i in range (number):
92         for a in range (nb_polylines):
93             for b in range (polylines_size):
94                 pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
95                                      random.randrange (-height, 2 * height))
96         cv.cvPolyLine (image, pt, 1,
97                        random_color (random),
98                        random.randrange (1, 9),
99                        line_type, 0)
100
101         highgui.cvShowImage (window_name, image)
102         highgui.cvWaitKey (delay)
103
104     # draw some filled polylines
105     for i in range (number):
106         for a in range (nb_polylines):
107             for b in range (polylines_size):
108                 pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
109                                      random.randrange (-height, 2 * height))
110         cv.cvFillPoly (image, pt,
111                        random_color (random),
112                        line_type, 0)
113
114         highgui.cvShowImage (window_name, image)
115         highgui.cvWaitKey (delay)
116
117     # draw some circles
118     for i in range (number):
119         pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
120                           random.randrange (-height, 2 * height))
121         cv.cvCircle (image, pt1, random.randrange (0, 300),
122                      random_color (random),
123                      random.randrange (-1, 9),
124                      line_type, 0)
125         
126         highgui.cvShowImage (window_name, image)
127         highgui.cvWaitKey (delay)
128
129     # draw some text
130     for i in range (number):
131         pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
132                           random.randrange (-height, 2 * height))
133         font = cv.cvInitFont (random.randrange (0, 8),
134                               random.randrange (0, 100) * 0.05 + 0.01,
135                               random.randrange (0, 100) * 0.05 + 0.01,
136                               random.randrange (0, 5) * 0.1,
137                               random.randrange (0, 10),
138                               line_type)
139
140         cv.cvPutText (image, "Testing text rendering!",
141                       pt1, font,
142                       random_color (random))
143         
144         highgui.cvShowImage (window_name, image)
145         highgui.cvWaitKey (delay)
146
147     # prepare a text, and get it's properties
148     font = cv.cvInitFont (cv.CV_FONT_HERSHEY_COMPLEX,
149                           3, 3, 0.0, 5, line_type)
150     text_size, ymin = cv.cvGetTextSize ("OpenCV forever!", font)
151     pt1.x = (width - text_size.width) / 2
152     pt1.y = (height + text_size.height) / 2
153     image2 = cv.cvCloneImage(image)
154
155     # now, draw some OpenCV pub ;-)
156     for i in range (255):
157         cv.cvSubS (image2, cv.cvScalarAll (i), image, None)
158         cv.cvPutText (image, "OpenCV forever!",
159                       pt1, font, cv.cvScalar (255, i, i))
160         highgui.cvShowImage (window_name, image)
161         highgui.cvWaitKey (delay)
162
163     # wait some key to end
164     highgui.cvWaitKey (0)