Mercurial > pygame
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 |
| 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 | 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 | 19 |
| 1492 | 20 def main(): |
| 21 pygame.init () | |
| 22 | |
| 23 pygame.display.set_mode ((255, 255)) | |
| 24 surface = pygame.Surface ((255, 255)) | |
| 25 | |
| 26 pygame.display.flip () | |
| 877 | 27 |
| 1492 | 28 # Create the PixelArray. |
| 29 ar = pygame.PixelArray (surface) | |
| 30 r, g, b = 0, 0, 0 | |
| 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 | 33 r, g, b = y, y, y |
| 34 ar[:,y] = (r, g, b) | |
| 35 del ar | |
| 36 show (surface) | |
| 877 | 37 |
| 1492 | 38 # We have made some gradient effect, now flip it. |
| 39 ar = pygame.PixelArray (surface) | |
| 40 ar[:] = ar[:,::-1] | |
| 41 del ar | |
| 42 show (surface) | |
|
852
4db78800a93a
A PixelArray class, which is meant to replace surfarray.
Rene Dudfield <renesd@gmail.com>
parents:
diff
changeset
|
43 |
| 1492 | 44 # Every second column will be made blue |
| 45 ar = pygame.PixelArray (surface) | |
| 46 ar[::2] = (0, 0, 255) | |
| 47 del ar | |
| 48 show (surface) | |
|
982
2c37905a2091
Added subscript support and make_surface() method to PixelArray.
marcus
parents:
888
diff
changeset
|
49 |
| 1492 | 50 # Every second row will be made green |
| 51 ar = pygame.PixelArray (surface) | |
| 52 ar[:,::2] = (0, 255, 0) | |
| 53 del ar | |
| 54 show (surface) | |
| 877 | 55 |
| 1492 | 56 # Manipulate the image. Flip it around the y axis. |
| 57 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp')) | |
| 58 ar = pygame.PixelArray (surface) | |
| 59 ar[:] = ar[:,::-1] | |
| 60 del ar | |
| 61 show (surface) | |
|
982
2c37905a2091
Added subscript support and make_surface() method to PixelArray.
marcus
parents:
888
diff
changeset
|
62 |
| 1492 | 63 # Flip the image around the x axis. |
| 64 ar = pygame.PixelArray (surface) | |
| 65 ar[:] = ar[::-1,:] | |
| 66 del ar | |
| 67 show (surface) | |
| 68 | |
| 69 # Every second column will be made white. | |
| 70 ar = pygame.PixelArray (surface) | |
| 71 ar[::2] = (255, 255, 255) | |
| 72 del ar | |
| 73 show (surface) | |
| 877 | 74 |
| 1492 | 75 # Flip the image around both axes, restoring it's original layout. |
| 76 ar = pygame.PixelArray (surface) | |
| 77 ar[:] = ar[::-1,::-1] | |
| 78 del ar | |
| 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 | 90 # Scale it by throwing each second pixel away. |
| 91 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp')) | |
| 92 ar = pygame.PixelArray (surface) | |
| 93 sf2 = ar[::2,::2].make_surface () | |
| 94 del ar | |
| 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 | 97 # Replace anything looking like the blue color from the text. |
| 98 ar = pygame.PixelArray (surface) | |
| 99 ar.replace ((60, 60, 255), (0, 255, 0), 0.06) | |
| 100 del ar | |
| 101 show (surface) | |
| 102 | |
| 103 # Extract anything which might be somewhat black. | |
| 104 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp')) | |
| 105 ar = pygame.PixelArray (surface) | |
| 106 ar2 = ar.extract ((0, 0, 0), 0.07) | |
| 107 sf2 = ar2.surface | |
| 108 del ar, ar2 | |
| 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 | 111 # Compare two images. |
| 112 surface = pygame.image.load (os.path.join (data_dir, 'alien1.gif')) | |
| 113 surface2 = pygame.image.load (os.path.join (data_dir, 'alien2.gif')) | |
| 114 ar1 = pygame.PixelArray (surface) | |
| 115 ar2 = pygame.PixelArray (surface2) | |
| 116 ar3 = ar1.compare (ar2, 0.07) | |
| 117 sf3 = ar3.surface | |
| 118 del ar1, ar2, ar3 | |
| 119 show (sf3) | |
| 1007 | 120 |
| 1492 | 121 if __name__ == '__main__': |
| 122 main() | |
| 123 |
