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
|
#!/usr/bin/env python
import os
try:
import pygame
# Use numeric, not numpy here (otherwise, follow the NUMPY comments)
# NUMPY: import numpy as N
import Numeric as N
from pygame.locals import *
surfarray = pygame.surfarray
if not surfarray: raise ImportError
except ImportError:
raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
pygame.init()
print 'Press the mouse button to advance image.'
print 'Press the "s" key to save the current image.'
# Guarantee the usage of Numeric
pygame.surfarray.use_arraytype ("numeric")
# NUMPY: pygame.surfarray.use_arraytype ("numpy")
def surfdemo_show(array_img, name):
"displays a surface, waits for user to continue"
screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
surfarray.blit_array(screen, array_img)
pygame.display.flip()
pygame.display.set_caption(name)
while 1:
e = pygame.event.wait()
if e.type == MOUSEBUTTONDOWN: break
elif e.type == KEYDOWN and e.key == K_s:
#pygame.image.save(screen, name+'.bmp')
#s = pygame.Surface(screen.get_size(), 0, 32)
#s = s.convert_alpha()
#s.fill((0,0,0,255))
#s.blit(screen, (0,0))
#s.fill((222,0,0,50), (0,0,40,40))
#pygame.image.save_extended(s, name+'.png')
#pygame.image.save(s, name+'.png')
#pygame.image.save(screen, name+'_screen.png')
#pygame.image.save(s, name+'.tga')
pygame.image.save(screen, name+'.png')
elif e.type == QUIT:
raise SystemExit
#allblack
allblack = N.zeros((128, 128))
surfdemo_show(allblack, 'allblack')
#striped
striped = N.zeros((128, 128, 3))
striped[:] = (255, 0, 0)
striped[:,::3] = (0, 255, 255)
surfdemo_show(striped, 'striped')
#imgarray
imagename = os.path.join('data', 'arraydemo.bmp')
imgsurface = pygame.image.load(imagename)
imgarray = surfarray.array2d(imgsurface)
surfdemo_show(imgarray, 'imgarray')
#flipped
flipped = imgarray[:,::-1]
surfdemo_show(flipped, 'flipped')
#scaledown
scaledown = imgarray[::2,::2]
surfdemo_show(scaledown, 'scaledown')
#scaleup
size = N.array(imgarray.shape)*2
scaleup = N.zeros(size)
scaleup[::2,::2] = imgarray
scaleup[1::2,::2] = imgarray
scaleup[:,1::2] = scaleup[:,::2]
surfdemo_show(scaleup, 'scaleup')
#redimg
rgbarray = surfarray.array3d(imgsurface)
redimg = N.array(rgbarray)
redimg[:,:,1:] = 0
surfdemo_show(redimg, 'redimg')
#soften
soften = N.array(rgbarray)*1
soften[1:,:] += rgbarray[:-1,:]*8
soften[:-1,:] += rgbarray[1:,:]*8
soften[:,1:] += rgbarray[:,:-1]*8
soften[:,:-1] += rgbarray[:,1:]*8
soften /= 33
surfdemo_show(soften, 'soften')
#crossfade (50%)
src = N.array(rgbarray)
dest = N.zeros(rgbarray.shape)
dest[:] = 20, 50, 100
diff = (dest - src) * 0.50
# NUMPY: xfade = src + diff.astype(N.uint)
xfade = src + diff.astype(N.Int)
surfdemo_show(xfade, 'xfade')
#alldone
|