def run(self, steps):
        x = self.state_in
        y = self.state_out
        hood = (3, 3)
        window_shape = (x.shape[0] - (hood[0] - 1), x.shape[1] - (hood[0] - 1))

        if self.draw:
            pygame.init()
            surfarray.use_arraytype('numpy')

        for a in range(steps):
            self.output(x)
            self.step += 1
            xx = stride_tricks.as_strided(x,
                                          shape=(window_shape[0],
                                                 window_shape[1], hood[0],
                                                 hood[1]),
                                          strides=x.strides + x.strides)

            for i in range(window_shape[0]):
                for j in range(window_shape[1]):
                    y[i + 1, j + 1] = self.rule(xx[i, j])

            x = y.copy()
        self.output(x)
        if self.draw:
            print "Done.. "
            time.sleep(1)
            #print "Done.. press 'q' to quit"
            #while 1:
            #    e = pygame.event.wait()
            #    if e.type == KEYDOWN and e.key == K_q:
            #        break
            pygame.quit()
    def run(self, steps):
        x = self.state_in
        y = self.state_out
        hood = (3,3)
        window_shape = (x.shape[0] - (hood[0] - 1), 
                        x.shape[1] - (hood[0] - 1))

        if self.draw:
            pygame.init()
            surfarray.use_arraytype('numpy')

        for a in range(steps):
            self.output(x)
            self.step += 1
            xx = stride_tricks.as_strided(x, 
                    shape=(window_shape[0], window_shape[1], hood[0], hood[1]), 
                    strides=x.strides + x.strides)

            for i in range(window_shape[0]):
                for j in range(window_shape[1]):
                    y[i+1,j+1] = self.rule(xx[i,j])

            x = y.copy()
        self.output(x)
        if self.draw:
            print "Done.. "
            time.sleep(1)
            #print "Done.. press 'q' to quit"
            #while 1:
            #    e = pygame.event.wait()
            #    if e.type == KEYDOWN and e.key == K_q: 
            #        break
            pygame.quit()
Example #3
0
def show_stimulus(stimulus, name='toto', resizable=True, exit=True, wait=0.0):
    """
    stimulus is a 4 dimension numpy array: [height, weight, frame, RGB]
    """
    from time import sleep

    h = stimulus.shape[0]
    w = stimulus.shape[1]
    f = stimulus.shape[2]
    surfarray.use_arraytype('numpy')
    pygame.init()
    pygame.display.set_caption(name)
    if (resizable == True): screen = pygame.display.set_mode((h, w), RESIZABLE)
    else: screen = pygame.display.set_mode((h, w))
    surface = pygame.Surface((h, w))
    looping = True
    i = 0
    while looping:
        if (i == f): i = 0
        surfarray.blit_array(surface, stimulus[:, :, i, :])
        screen.blit(surface, (0, 0))
        pygame.display.flip()
        event = pygame.event.poll()
        looping = quit(event, exit=exit)
        i += 1
        sleep(wait)
Example #4
0
def show_stimulus(stimulus, name='toto', resizable=True):
    """
    stimulus is a 4 dimension numpy array: [height, weight, frame, RGB]
    """
    h = stimulus.shape[0]
    w = stimulus.shape[1]
    f = stimulus.shape[2]
    surfarray.use_arraytype('numpy')
    pygame.init()
    pygame.display.set_caption(name)
    if (resizable == True): screen = pygame.display.set_mode((h, w), RESIZABLE)
    else: screen = pygame.display.set_mode((h, w))
    surface = pygame.Surface((h, w))
    looping = True
    i = 0
    while looping:
        if (i == f): i = 0
        surfarray.blit_array(surface, stimulus[:, :, i, :])
        screen.blit(surface, (0, 0))
        pygame.display.flip()
        event = pygame.event.poll()
        looping = quit(event)
        i += 1
Example #5
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, uint8, uint
    else:
        import Numeric as N
        from Numeric import Int32 as int32, UInt8 as uint8, UInt as uint

    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
    #having factor as an array forces integer upgrade during multiplication
    #of rgbarray, even for numpy.
    factor = N.array((8,), int32)
    soften = N.array(rgbarray, int32)
    soften[1:,:]  += rgbarray[:-1,:] * factor
    soften[:-1,:] += rgbarray[1:,:] * factor
    soften[:,1:]  += rgbarray[:,:-1] * factor
    soften[:,:-1] += rgbarray[:,1:] * factor
    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
    xfade = src + diff.astype(uint)
    surfdemo_show(xfade, 'xfade')




    #alldone
    pygame.quit()
Example #6
0
import morphology
import shape
import threshold

done = False
lock = threading.Lock()
pool = []
display = True
display_option = 1
center_display = False
degree_display = False
o_thr = False
o_color = 'r'
o_sides = 5
prev_threshold = None
surfarray.use_arraytype('numpy')
size = (180, 180)
camera = picamera.PiCamera()
target_found = False
frame_count = 0
hardware.ready()

print "                                         ###"
print "                                     ## ##### ##"
print "                                    ## ### ### ##"
print "                                   ###  ## ##  ###"
print "                                    ##   ###   ##"
print "                                #    ##   #   ##"
print "                               #        ## ##"
print "                             #     #   ##   ##"
print "                           #     #"
Example #7
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 as 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()
Example #8
0
from pygame import surfarray, image, display
import pygame

pygame.init()
image = image.load('img.png')
resolution = (image.get_width(), image.get_height())
screen = display.set_mode(resolution)
screen.blit(image,(0,0))
surfarray.use_arraytype("numpy")
screenpix = surfarray.pixels3d(image)

data = []
data_str = ''
for y in range(resolution[1]):
    for x in range(resolution[0]):
        red = screenpix[x][y][0]
        green = screenpix[x][y][1]
        blue = screenpix[x][y][2] 
        data.append([red / 255.0 , green / 255.0 ,blue / 255.0])
        data_str += str([red / 255.0 , green / 255.0 ,blue / 255.0]) + ',' + '\n'
with open('rgb.js', 'w') as f:
    f.write('var labels = [' + data_str + '];') #[[1,1,1],[0,0,0],...]
Example #9
0
import os.path
import re, random
import pygame
import pygame.surfarray as surfarray
surfarray.use_arraytype('numpy')
import numpy as np

def palettify(infile, outfile):
	surface = pygame.image.load(infile)
	palette = ([(i,i,i) for i in range(3,259,4)] +
				[(i,0,0) for i in range(3,259,4)] +
				[(i,i,0) for i in range(3,259,4)] +
				[(0,i,0) for i in range(3,259,4)])
	palette[0] = (0,0,255)

	s = pygame.surface.Surface(surface.get_size(), depth=8)
	s.fill(0)
	s.set_palette(palette)
	s.blit(surface,(0,0))
	pygame.image.save(s, outfile)
 

def palettify_bogus(infile, outfile):
	surface = pygame.image.load(infile).convert(24)
	a = surfarray.pixels3d(surface)

	realgreens = (a[...,0]==0) & (a[...,2]==0)
	realreds = (a[...,1]==0) & (a[...,2]==0)
	realyellows = (a[...,0]==a[...,1]) & (a[...,2]==0)
	realblues = (a[...,0]==0) & (a[...,1]==0) & (a[...,2]==255)