~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to examples/arraydemo.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
try:
 
3
    import pygame
 
4
    import Numeric as N
 
5
    from pygame.locals import *
 
6
    surfarray = pygame.surfarray
 
7
    if not surfarray: raise ImportError
 
8
except ImportError:
 
9
    raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
 
10
 
 
11
 
 
12
pygame.init()
 
13
print 'Press the mouse button to advance image.'
 
14
print 'Press the "s" key to save the current image.'
 
15
 
 
16
 
 
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)
 
21
    pygame.display.flip()
 
22
    pygame.display.set_caption(name)
 
23
    while 1:
 
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
 
29
 
 
30
 
 
31
 
 
32
 
 
33
#allblack
 
34
allblack = N.zeros((128, 128))
 
35
surfdemo_show(allblack, 'allblack')
 
36
 
 
37
 
 
38
#striped
 
39
striped = N.zeros((128, 128, 3))
 
40
striped[:] = (255, 0, 0)
 
41
striped[:,::3] = (0, 255, 255)
 
42
surfdemo_show(striped, 'striped')
 
43
 
 
44
 
 
45
#imgarray
 
46
imagename = os.path.join('data', 'arraydemo.bmp')
 
47
imgsurface = pygame.image.load(imagename)
 
48
imgarray = surfarray.array2d(imgsurface)
 
49
surfdemo_show(imgarray, 'imgarray')
 
50
 
 
51
 
 
52
#flipped
 
53
flipped = imgarray[:,-1:0:-1]
 
54
surfdemo_show(flipped, 'flipped')
 
55
 
 
56
 
 
57
#scaledown
 
58
scaledown = imgarray[::2,::2]
 
59
surfdemo_show(scaledown, 'scaledown')
 
60
 
 
61
 
 
62
#scaleup
 
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')
 
69
 
 
70
 
 
71
#redimg
 
72
rgbarray = surfarray.array3d(imgsurface)
 
73
redimg = N.array(rgbarray)
 
74
redimg[:,:,1:] = 0
 
75
surfdemo_show(redimg, 'redimg')
 
76
 
 
77
 
 
78
#soften
 
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
 
84
soften /= 33
 
85
surfdemo_show(soften, 'soften')
 
86
 
 
87
 
 
88
#crossfade (50%)
 
89
src = N.array(rgbarray)
 
90
dest = N.zeros(rgbarray.shape)
 
91
dest[:] = 20, 50, 100
 
92
diff = (dest - src) * 0.50
 
93
xfade = src + diff.astype(N.Int)
 
94
surfdemo_show(xfade, 'xfade')
 
95
 
 
96
 
 
97
 
 
98
#alldone