File: pixelarray.py

package info (click to toggle)
pygame 2.6.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,076 kB
  • sloc: ansic: 66,932; python: 48,797; javascript: 1,153; objc: 224; sh: 121; makefile: 59; cpp: 25
file content (142 lines) | stat: -rw-r--r-- 3,453 bytes parent folder | download | duplicates (2)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
""" pygame.examples.pixelarray

PixelArray does array processing of pixels.
Sort of like another array processor called 'numpy' - But for pixels.

    Flip it,
            stripe it,
                      rotate it.

Controls
--------

To see different effects - press a key or click a mouse.
"""
import os
import pygame as pg


main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, "data")


def show(image):
    screen = pg.display.get_surface()
    screen.fill((255, 255, 255))
    screen.blit(image, (0, 0))
    pg.display.flip()
    while True:
        event = pg.event.wait()
        if event.type == pg.QUIT:
            pg.quit()
            raise SystemExit
        if event.type in [pg.MOUSEBUTTONDOWN, pg.KEYDOWN]:
            break


def main():
    pg.init()

    pg.display.set_mode((255, 255))
    surface = pg.Surface((255, 255))

    pg.display.flip()

    # Create the PixelArray.
    ar = pg.PixelArray(surface)

    # Do some easy gradient effect.
    for y in range(255):
        r, g, b = y, y, y
        ar[:, y] = (r, g, b)
    del ar
    show(surface)

    # We have made some gradient effect, now flip it.
    ar = pg.PixelArray(surface)
    ar[:] = ar[:, ::-1]
    del ar
    show(surface)

    # Every second column will be made blue
    ar = pg.PixelArray(surface)
    ar[::2] = (0, 0, 255)
    del ar
    show(surface)

    # Every second row will be made green
    ar = pg.PixelArray(surface)
    ar[:, ::2] = (0, 255, 0)
    del ar
    show(surface)

    # Manipulate the image. Flip it around the y axis.
    surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp"))
    ar = pg.PixelArray(surface)
    ar[:] = ar[:, ::-1]
    del ar
    show(surface)

    # Flip the image around the x axis.
    ar = pg.PixelArray(surface)
    ar[:] = ar[::-1, :]
    del ar
    show(surface)

    # Every second column will be made white.
    ar = pg.PixelArray(surface)
    ar[::2] = (255, 255, 255)
    del ar
    show(surface)

    # Flip the image around both axes, restoring its original layout.
    ar = pg.PixelArray(surface)
    ar[:] = ar[::-1, ::-1]
    del ar
    show(surface)

    # Rotate 90 degrees clockwise.
    w, h = surface.get_size()
    surface2 = pg.Surface((h, w), surface.get_flags(), surface)
    ar = pg.PixelArray(surface)
    ar2 = pg.PixelArray(surface2)
    ar2[...] = ar.transpose()[::-1, :]
    del ar, ar2
    show(surface2)

    # Scale it by throwing each second pixel away.
    surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp"))
    ar = pg.PixelArray(surface)
    sf2 = ar[::2, ::2].make_surface()
    del ar
    show(sf2)

    # Replace anything looking like the blue color from the text.
    ar = pg.PixelArray(surface)
    ar.replace((60, 60, 255), (0, 255, 0), 0.06)
    del ar
    show(surface)

    # Extract anything which might be somewhat black.
    surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp"))
    ar = pg.PixelArray(surface)
    ar2 = ar.extract((0, 0, 0), 0.07)
    sf2 = ar2.surface
    del ar, ar2
    show(sf2)

    # Compare two images.
    surface = pg.image.load(os.path.join(data_dir, "alien1.gif"))
    surface2 = pg.image.load(os.path.join(data_dir, "alien2.gif"))
    ar1 = pg.PixelArray(surface)
    ar2 = pg.PixelArray(surface2)
    ar3 = ar1.compare(ar2, 0.07)
    sf3 = ar3.surface
    del ar1, ar2, ar3
    show(sf3)


if __name__ == "__main__":
    main()
    pg.quit()