annotate examples/pixelarray.py @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents 2fbf3c27fdaa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1031
39e462f3ad75 added #\!/usr/bin/env python to the top of all examples that didn't have it.
Rene Dudfield <renesd@gmail.com>
parents: 1007
diff changeset
1 #!/usr/bin/env python
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
2 import os, pygame
1578
c0d170c1e636 all standard modules build with Python 3.1. Optional movie module adapted. setup.py skips unsupported files for Python 3
Lenard Lindstrom <len-l@telus.net>
parents: 1492
diff changeset
3 from pygame.compat import xrange_
852
4db78800a93a A PixelArray class, which is meant to replace surfarray.
Rene Dudfield <renesd@gmail.com>
parents:
diff changeset
4
1427
a6e94e964a69 examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents: 1031
diff changeset
5 main_dir = os.path.split(os.path.abspath(__file__))[0]
a6e94e964a69 examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents: 1031
diff changeset
6 data_dir = os.path.join(main_dir, 'data')
a6e94e964a69 examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents: 1031
diff changeset
7
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
8 def show (image):
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
9 screen = pygame.display.get_surface()
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 982
diff changeset
10 screen.fill ((255, 255, 255))
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
11 screen.blit (image, (0, 0))
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
12 pygame.display.flip ()
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
13 while 1:
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
14 event = pygame.event.wait ()
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
15 if event.type == pygame.QUIT:
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
16 raise SystemExit
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
17 if event.type == pygame.MOUSEBUTTONDOWN:
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
18 break
877
b46c37340132 Improved PixelArray docs, unittests and example
marcus
parents: 866
diff changeset
19
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
20 def main():
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
21 pygame.init ()
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
22
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
23 pygame.display.set_mode ((255, 255))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
24 surface = pygame.Surface ((255, 255))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
25
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
26 pygame.display.flip ()
877
b46c37340132 Improved PixelArray docs, unittests and example
marcus
parents: 866
diff changeset
27
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
28 # Create the PixelArray.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
29 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
30 r, g, b = 0, 0, 0
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
31 # Do some easy gradient effect.
1578
c0d170c1e636 all standard modules build with Python 3.1. Optional movie module adapted. setup.py skips unsupported files for Python 3
Lenard Lindstrom <len-l@telus.net>
parents: 1492
diff changeset
32 for y in xrange_ (255):
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
33 r, g, b = y, y, y
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
34 ar[:,y] = (r, g, b)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
35 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
36 show (surface)
877
b46c37340132 Improved PixelArray docs, unittests and example
marcus
parents: 866
diff changeset
37
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
38 # We have made some gradient effect, now flip it.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
39 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
40 ar[:] = ar[:,::-1]
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
41 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
42 show (surface)
852
4db78800a93a A PixelArray class, which is meant to replace surfarray.
Rene Dudfield <renesd@gmail.com>
parents:
diff changeset
43
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
44 # Every second column will be made blue
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
45 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
46 ar[::2] = (0, 0, 255)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
47 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
48 show (surface)
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
49
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
50 # Every second row will be made green
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
51 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
52 ar[:,::2] = (0, 255, 0)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
53 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
54 show (surface)
877
b46c37340132 Improved PixelArray docs, unittests and example
marcus
parents: 866
diff changeset
55
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
56 # Manipulate the image. Flip it around the y axis.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
57 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
58 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
59 ar[:] = ar[:,::-1]
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
60 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
61 show (surface)
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
62
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
63 # Flip the image around the x axis.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
64 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
65 ar[:] = ar[::-1,:]
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
66 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
67 show (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
68
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
69 # Every second column will be made white.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
70 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
71 ar[::2] = (255, 255, 255)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
72 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
73 show (surface)
877
b46c37340132 Improved PixelArray docs, unittests and example
marcus
parents: 866
diff changeset
74
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
75 # Flip the image around both axes, restoring it's original layout.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
76 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
77 ar[:] = ar[::-1,::-1]
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
78 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
79 show (surface)
982
2c37905a2091 Added subscript support and make_surface() method to PixelArray.
marcus
parents: 888
diff changeset
80
2244
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
81 # Rotate 90 degrees clockwise.
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
82 w, h = surface.get_size ()
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
83 surface2 = pygame.Surface ((h, w), surface.get_flags (), surface)
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
84 ar = pygame.PixelArray (surface)
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
85 ar2 = pygame.PixelArray (surface2)
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
86 ar2[...] = ar.transpose ()[::-1,:]
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
87 del ar, ar2
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
88 show (surface2)
2fbf3c27fdaa Incidental PixelArray updates
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
89
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
90 # Scale it by throwing each second pixel away.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
91 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
92 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
93 sf2 = ar[::2,::2].make_surface ()
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
94 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
95 show (sf2)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 982
diff changeset
96
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
97 # Replace anything looking like the blue color from the text.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
98 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
99 ar.replace ((60, 60, 255), (0, 255, 0), 0.06)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
100 del ar
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
101 show (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
102
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
103 # Extract anything which might be somewhat black.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
104 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
105 ar = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
106 ar2 = ar.extract ((0, 0, 0), 0.07)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
107 sf2 = ar2.surface
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
108 del ar, ar2
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
109 show (sf2)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 982
diff changeset
110
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
111 # Compare two images.
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
112 surface = pygame.image.load (os.path.join (data_dir, 'alien1.gif'))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
113 surface2 = pygame.image.load (os.path.join (data_dir, 'alien2.gif'))
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
114 ar1 = pygame.PixelArray (surface)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
115 ar2 = pygame.PixelArray (surface2)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
116 ar3 = ar1.compare (ar2, 0.07)
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
117 sf3 = ar3.surface
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
118 del ar1, ar2, ar3
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
119 show (sf3)
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 993
diff changeset
120
1492
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
121 if __name__ == '__main__':
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
122 main()
430243520809 some examples cleanup
Lenard Lindstrom <len-l@telus.net>
parents: 1427
diff changeset
123