Mercurial > pygame
annotate examples/pixelarray.py @ 1849:301bc61643a9 math_module
merged changes from trunk
| author | lorenz |
|---|---|
| date | Mon, 16 Nov 2009 18:31:40 +0000 |
| parents | c0d170c1e636 |
| children | 2fbf3c27fdaa |
| 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 |
| 1492 | 81 # Scale it by throwing each second pixel away. |
| 82 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp')) | |
| 83 ar = pygame.PixelArray (surface) | |
| 84 sf2 = ar[::2,::2].make_surface () | |
| 85 del ar | |
| 86 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
|
87 |
| 1492 | 88 # Replace anything looking like the blue color from the text. |
| 89 ar = pygame.PixelArray (surface) | |
| 90 ar.replace ((60, 60, 255), (0, 255, 0), 0.06) | |
| 91 del ar | |
| 92 show (surface) | |
| 93 | |
| 94 # Extract anything which might be somewhat black. | |
| 95 surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp')) | |
| 96 ar = pygame.PixelArray (surface) | |
| 97 ar2 = ar.extract ((0, 0, 0), 0.07) | |
| 98 sf2 = ar2.surface | |
| 99 del ar, ar2 | |
| 100 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
|
101 |
| 1492 | 102 # Compare two images. |
| 103 surface = pygame.image.load (os.path.join (data_dir, 'alien1.gif')) | |
| 104 surface2 = pygame.image.load (os.path.join (data_dir, 'alien2.gif')) | |
| 105 ar1 = pygame.PixelArray (surface) | |
| 106 ar2 = pygame.PixelArray (surface2) | |
| 107 ar3 = ar1.compare (ar2, 0.07) | |
| 108 sf3 = ar3.surface | |
| 109 del ar1, ar2, ar3 | |
| 110 show (sf3) | |
| 1007 | 111 |
| 1492 | 112 if __name__ == '__main__': |
| 113 main() | |
| 114 |
