1#!/usr/bin/env python 2""" pygame.examples.pixelarray 3 4PixelArray does array processing of pixels. 5Sort of like another array processor called 'numpy' - But for pixels. 6 7 Flip it, 8 stripe it, 9 rotate it. 10 11Controls 12-------- 13 14To see different effects - press a key or click a mouse. 15""" 16import os 17import pygame as pg 18from pygame.compat import xrange_ 19 20main_dir = os.path.split(os.path.abspath(__file__))[0] 21data_dir = os.path.join(main_dir, "data") 22 23 24def show(image): 25 screen = pg.display.get_surface() 26 screen.fill((255, 255, 255)) 27 screen.blit(image, (0, 0)) 28 pg.display.flip() 29 while 1: 30 event = pg.event.wait() 31 if event.type == pg.QUIT: 32 pg.quit() 33 raise SystemExit 34 if event.type in [pg.MOUSEBUTTONDOWN, pg.KEYDOWN]: 35 break 36 37 38def main(): 39 pg.init() 40 41 pg.display.set_mode((255, 255)) 42 surface = pg.Surface((255, 255)) 43 44 pg.display.flip() 45 46 # Create the PixelArray. 47 ar = pg.PixelArray(surface) 48 49 # Do some easy gradient effect. 50 for y in xrange_(255): 51 r, g, b = y, y, y 52 ar[:, y] = (r, g, b) 53 del ar 54 show(surface) 55 56 # We have made some gradient effect, now flip it. 57 ar = pg.PixelArray(surface) 58 ar[:] = ar[:, ::-1] 59 del ar 60 show(surface) 61 62 # Every second column will be made blue 63 ar = pg.PixelArray(surface) 64 ar[::2] = (0, 0, 255) 65 del ar 66 show(surface) 67 68 # Every second row will be made green 69 ar = pg.PixelArray(surface) 70 ar[:, ::2] = (0, 255, 0) 71 del ar 72 show(surface) 73 74 # Manipulate the image. Flip it around the y axis. 75 surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp")) 76 ar = pg.PixelArray(surface) 77 ar[:] = ar[:, ::-1] 78 del ar 79 show(surface) 80 81 # Flip the image around the x axis. 82 ar = pg.PixelArray(surface) 83 ar[:] = ar[::-1, :] 84 del ar 85 show(surface) 86 87 # Every second column will be made white. 88 ar = pg.PixelArray(surface) 89 ar[::2] = (255, 255, 255) 90 del ar 91 show(surface) 92 93 # Flip the image around both axes, restoring it's original layout. 94 ar = pg.PixelArray(surface) 95 ar[:] = ar[::-1, ::-1] 96 del ar 97 show(surface) 98 99 # Rotate 90 degrees clockwise. 100 w, h = surface.get_size() 101 surface2 = pg.Surface((h, w), surface.get_flags(), surface) 102 ar = pg.PixelArray(surface) 103 ar2 = pg.PixelArray(surface2) 104 ar2[...] = ar.transpose()[::-1, :] 105 del ar, ar2 106 show(surface2) 107 108 # Scale it by throwing each second pixel away. 109 surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp")) 110 ar = pg.PixelArray(surface) 111 sf2 = ar[::2, ::2].make_surface() 112 del ar 113 show(sf2) 114 115 # Replace anything looking like the blue color from the text. 116 ar = pg.PixelArray(surface) 117 ar.replace((60, 60, 255), (0, 255, 0), 0.06) 118 del ar 119 show(surface) 120 121 # Extract anything which might be somewhat black. 122 surface = pg.image.load(os.path.join(data_dir, "arraydemo.bmp")) 123 ar = pg.PixelArray(surface) 124 ar2 = ar.extract((0, 0, 0), 0.07) 125 sf2 = ar2.surface 126 del ar, ar2 127 show(sf2) 128 129 # Compare two images. 130 surface = pg.image.load(os.path.join(data_dir, "alien1.gif")) 131 surface2 = pg.image.load(os.path.join(data_dir, "alien2.gif")) 132 ar1 = pg.PixelArray(surface) 133 ar2 = pg.PixelArray(surface2) 134 ar3 = ar1.compare(ar2, 0.07) 135 sf3 = ar3.surface 136 del ar1, ar2, ar3 137 show(sf3) 138 139 140if __name__ == "__main__": 141 main() 142 pg.quit() 143