arraydemo.py :  » Game-2D-3D » Pygame » pygame-1.9.1release » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Pygame 
Pygame » pygame 1.9.1release » examples » arraydemo.py
#!/usr/bin/env python

import sys
import os
try:
    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():
            surfarray.use_arraytype('numpy')
        elif 'numeric' in surfarray.get_arraytype():
            surfarray.use_arraytype('numeric')
        else:
            raise ImportError('No array package is installed')
    else:
        surfarray.use_arraytype(arraytype)

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

    pygame.init()
    print ('Using %s' % 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)
        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), int32)
    surfdemo_show(allblack, 'allblack')


    #striped
    #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')


    #imgarray
    imagename = os.path.join(main_dir, '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
    #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')


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




    #alldone
    pygame.quit()

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:
            main('numpy')
        elif '--numeric' in sys.argv:
            main('numeric')
        else:
            usage()
    elif len(sys.argv) == 1:
        main()
    else:
        usage()

        

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.