pygame / examples /

#!/usr/bin/env python

import sys
import os
    import pygame
    from pygame import surfarray
    from pygame.locals import *
except ImportError:
    raise ImportError, 'Error Importing Pygame/surfarray'

main_dir = os.path.split(os.path.abspath(__file__))[0]

def main(arraytype=None):
    """show various surfarray effects

    If arraytype is provided then use that array package. Valid
    values are 'numeric' or 'numpy'. Otherwise default to NumPy,
    or fall back on Numeric if NumPy is not installed.


    if arraytype is None:
        if 'numpy' in surfarray.get_arraytypes():
        elif 'numeric' in surfarray.get_arraytype():
            raise ImportError, 'No array package is installed'

    if surfarray.get_arraytype() == 'numpy':
        import numpy as N
        from numpy import int32
        import Numeric as N
        from Numeric import Int32 as int32

    print 'Using', surfarray.get_arraytype().capitalize()
    print 'Press the mouse button to advance image.'
    print 'Press the "s" key to save the current image.'

    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)
        while 1:
            e = pygame.event.wait()
            if e.type == MOUSEBUTTONDOWN: break
            elif e.type == KEYDOWN and e.key == K_s:
      , name+'.bmp')
                #s = pygame.Surface(screen.get_size(), 0, 32)
                #s = s.convert_alpha()
                #s.blit(screen, (0,0))
                #s.fill((222,0,0,50), (0,0,40,40))
                #pygame.image.save_extended(s, name+'.png')
      , name+'.png')
      , name+'_screen.png')
      , name+'.tga')
      , name+'.png')
            elif e.type == QUIT:
                raise SystemExit

    allblack = N.zeros((128, 128), int32)
    surfdemo_show(allblack, 'allblack')

    #the element type is required for N.zeros in  NumPy else
    #an array of float is returned.
    striped = N.zeros((128, 128, 3), int32)
    striped[:] = (255, 0, 0)
    striped[:,::3] = (0, 255, 255)
    surfdemo_show(striped, 'striped')

    imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp')
    imgsurface = pygame.image.load(imagename)
    imgarray = surfarray.array2d(imgsurface)
    surfdemo_show(imgarray, 'imgarray')

    flipped = imgarray[:,::-1]
    surfdemo_show(flipped, 'flipped')

    scaledown = imgarray[::2,::2]
    surfdemo_show(scaledown, 'scaledown')

    #the element type is required for N.zeros in NumPy else
    #an #array of floats is returned.
    size = N.array(imgarray.shape)*2
    scaleup = N.zeros(size, int32)
    scaleup[::2,::2] = imgarray
    scaleup[1::2,::2] = imgarray
    scaleup[:,1::2] = scaleup[:,::2]
    surfdemo_show(scaleup, 'scaleup')

    rgbarray = surfarray.array3d(imgsurface)
    redimg = N.array(rgbarray)
    redimg[:,:,1:] = 0
    surfdemo_show(redimg, 'redimg')

    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
    if surfarray.get_arraytype() == 'numpy':
        xfade = src + diff.astype(N.uint)
        xfade = src + diff.astype(N.Int)
    surfdemo_show(xfade, 'xfade')


def usage():
    print "Usage: command line option [--numpy|--numeric]"
    print "  The default is to use NumPy if installed,"
    print "  otherwise Numeric"

if __name__ == '__main__':
    if len(sys.argv) == 2:
        if '--numpy' in sys.argv:
        elif '--numeric' in sys.argv:
    elif len(sys.argv) == 1:

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.