5
from pygame.locals import *
6
surfarray = pygame.surfarray
7
if not surfarray: raise ImportError
9
raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
13
print 'Press the mouse button to advance image.'
14
print 'Press the "s" key to save the current image.'
17
def surfdemo_show(array_img, name):
18
"displays a surface, waits for user to continue"
19
screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
20
surfarray.blit_array(screen, array_img)
22
pygame.display.set_caption(name)
24
e = pygame.event.poll()
25
if e.type == MOUSEBUTTONDOWN: break
26
elif e.type == KEYDOWN and e.key == K_s:
27
pygame.image.save(screen, name+'.bmp')
28
elif e.type == QUIT: raise SystemExit
34
allblack = N.zeros((128, 128))
35
surfdemo_show(allblack, 'allblack')
39
striped = N.zeros((128, 128, 3))
40
striped[:] = (255, 0, 0)
41
striped[:,::3] = (0, 255, 255)
42
surfdemo_show(striped, 'striped')
46
imagename = os.path.join('data', 'arraydemo.bmp')
47
imgsurface = pygame.image.load(imagename)
48
imgarray = surfarray.array2d(imgsurface)
49
surfdemo_show(imgarray, 'imgarray')
53
flipped = imgarray[:,-1:0:-1]
54
surfdemo_show(flipped, 'flipped')
58
scaledown = imgarray[::2,::2]
59
surfdemo_show(scaledown, 'scaledown')
63
size = N.array(imgarray.shape)*2
64
scaleup = N.zeros(size)
65
scaleup[::2,::2] = imgarray
66
scaleup[1::2,::2] = imgarray
67
scaleup[:,1::2] = scaleup[:,::2]
68
surfdemo_show(scaleup, 'scaleup')
72
rgbarray = surfarray.array3d(imgsurface)
73
redimg = N.array(rgbarray)
75
surfdemo_show(redimg, 'redimg')
79
soften = N.array(rgbarray)*1
80
soften[1:,:] += rgbarray[:-1,:]*8
81
soften[:-1,:] += rgbarray[1:,:]*8
82
soften[:,1:] += rgbarray[:,:-1]*8
83
soften[:,:-1] += rgbarray[:,1:]*8
85
surfdemo_show(soften, 'soften')
89
src = N.array(rgbarray)
90
dest = N.zeros(rgbarray.shape)
92
diff = (dest - src) * 0.50
93
xfade = src + diff.astype(N.Int)
94
surfdemo_show(xfade, 'xfade')