Package pype :: Module spritetools
[frames] | no frames]

Module spritetools

source code

Supplemental sprite functions.

These are mostly generator functions that fill sprites with useful pixel patterns: gratings of various times, Gaussian envelopes etc. It extends the basic functionality provided by the sprite module and most functions work on existing Sprite objects by modding the underlying image data.

Author -- James A. Mazer (mazerj@gmail.com)

Classes
  SpritetoolsError
Functions
 
unpack_rgb(color, R, G, B) source code
 
g2rgb(a)
Convert an [WxHx1] grayscale image into a [WxHx3] RGB array.
source code
 
pixelize(a, rgb=None, norm=1)
Convert a floating point array into an np.uint8 array.
source code
 
genpolar(w, h=None, typecode=np.float64, degrees=False)
Generate polar axes (like polar meshgrid)
source code
 
gray2rgb8(g, inrange=(-1.0,1.0))
Convert grayscale image array to 8bit integer array.
source code
 
rgb2rgb8(r, g, b, inrange=(-1.0,1.0))
Convert rgb image data to 8bit integer array.
source code
 
singrat(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D sine grating generator (odd symmetric).
source code
 
cosgrat(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D cosine grating generator (even symmetric).
source code
 
singrat2(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None, xcache=None)
CACHING version of singrat
source code
 
cosgrat2(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None, xcache=None)
CACHING version of cosgrat
source code
 
polargrat(s, cfreq, rfreq, phase_deg, polarity, R=1.0, G=1.0, B=1.0, logpolar=False, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D polar (non-Cartesian) grating generator.
source code
 
logpolargrat(s, cfreq, rfreq, phase_deg, polarity, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D log polar (non-Cartesian) grating generator
source code
 
hypergrat(s, freq, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D hyperbolic (non-Cartesian) grating generator.
source code
 
hartley(s, kx, ky, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, color=None)
Hartley basis function generator (after Ringach et al, 1997)
source code
 
gabor(s, frequency, phase_deg, ori_deg, sigma, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)
2D gabor generator.
source code
 
uniformnoise(s, binary=False, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, color=None)
Fill sprite with uniform white noise
source code
 
gaussiannoise(s, R=1.0, G=1.0, B=1.0, meanlum=0.5, stddev=1.0, color=None)
Fill sprite with Gaussian white noise
source code
 
alphabar(s, bw, bh, ori_deg, R=1.0, G=1.0, B=1.0)
Generate a bar into existing sprite using the alpha channel.
source code
 
alpha_gaussian(s, xsigma, ysigma=None, ori_deg=0.0)
Generate symmetric and asymmetric Gaussian envelopes into the alpha channel.
source code
 
alphaGaussian(s, xsigma, ysigma=None, ori_deg=0.0)
Generate symmetric and asymmetric Gaussian envelopes into the alpha channel.
source code
 
benchmark(fb) source code
Variables
  alphaGaussian2 = obsolete_fn
  gaussianEnvelope = obsolete_fn
Function Details

g2rgb(a)

source code 
Convert an [WxHx1] grayscale image into a [WxHx3] RGB array.
Parameters:
  • a - (array) monochrome input image
Returns:
(array) RGB output image

pixelize(a, rgb=None, norm=1)

source code 
Convert a floating point array into an np.uint8 array.
Parameters:
  • a - (array) array to be converted
  • rgb - if true, then promote from 1 plane to 3 planes using g2rgb
  • norm - (boolean) if true, scale min-max into range 1-255
Returns:
pixelized version of input array; result is suitable for assigning to <sprite>.alpha or <sprite>.array.

genpolar(w, h=None, typecode=np.float64, degrees=False)

source code 
Generate polar axes (like polar meshgrid)
Parameters:
  • w, h - width and height of sprite (height defaults to width)
  • typecode - output type, defaults to float64 ('d')
  • degrees - True/False
Returns:
(array) r and theta arrays

gray2rgb8(g, inrange=(-1.0,1.0))

source code 
Convert grayscale image array to 8bit integer array.
Parameters:
  • g - (numpy array) gray scale image array
  • inrange - (float pair) min/max values for input image
Returns:
(numpy array) uint8 RGB array

rgb2rgb8(r, g, b, inrange=(-1.0,1.0))

source code 
Convert rgb image data to 8bit integer array.
Parameters:
  • r, g, b - (numpy arrays) red, green and blue image planes
  • inrange - (float pair) min/max values for input image
Returns:
(numpy array) uint8 RGB array

singrat(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 

2D sine grating generator (odd symmetric).

NB Verified frequency is really cycles/sprite JM 17-sep-2006.

Parameters:
  • s - (Sprite) target sprite
  • frequency - frequency in cycles/sprite (or cyc/deg, if ppd is given)
  • phase_deg - (degrees) (nb: 0deg phase centers the sine function at sprite ctr)
  • ori_deg - (degrees) grating orientation
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/degree) if specified, then it means that freq is being specified in cycles/degree
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

cosgrat(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 

2D cosine grating generator (even symmetric).

NB Verified frequency is really cycles/sprite JM 17-sep-2006.

Parameters:
  • s - (Sprite) target sprite
  • frequency - frequency in cycles/sprite
  • phase_deg - (degrees) (nb: 0deg phase centers the cosine function at sprite ctr)
  • ori_deg - (degrees) grating orientation
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/degree) if specified, then it means that freq is being specified in cycles/degree
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

singrat2(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None, xcache=None)

source code 

CACHING version of singrat

This is identical to singrat(), but will cache the coordinate system (based on ori_deg) in a dictionary for fast retrival. This can really speed things up when generating a large number of gratings that differ only in phase or sf.

If you don't need caching, don't use this!

Parameters:
  • xcache - (dict) the actual cache
Returns:
(dict) updated cache

cosgrat2(s, frequency, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None, xcache=None)

source code 

CACHING version of cosgrat

This is identical to cosgrat(), but will cache the coordinate system (based on ori_deg) in a dictionary for fast retrival. This can really speed things up when generating a large number of gratings that differ only in phase or sf.

If you don't need caching, don't use this!

Parameters:
  • xcache - (dict) the actual cache
Returns:
(dict) updated cache

polargrat(s, cfreq, rfreq, phase_deg, polarity, R=1.0, G=1.0, B=1.0, logpolar=False, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 

2D polar (non-Cartesian) grating generator.

NB Verified frequencies are really cycles/sprite JM 17-sep-2006.

Parameters:
  • s - (Sprite) target sprite
  • cfreq - concentric frequency (cycles/sprite or cyc/deg - see ppd)
  • rfreq - concentric frequency (cycles/360deg)
  • phase_deg - (degrees)
  • polarity - 0 or 1 -> really just a 180 deg phase shift
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/degree); if specified, then it means that freq is being specified in cycles/degree - for cfreq only
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)

logpolargrat(s, cfreq, rfreq, phase_deg, polarity, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 

2D log polar (non-Cartesian) grating generator

NB Frequencies are in cycles/sprite or cycles/360deg

NB Verified frequenies are really cycles/sprite JM 17-sep-2006.

Parameters:
  • s - (Sprite) target sprite
  • cfreq - concentric frequency (cycles/sprite or cycles/deg see ppd)
  • rfreq - concentric frequency (cycles/360deg)
  • phase_deg - (degrees)
  • polarity - 0 or 1 -> really just a 180 deg phase shift
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/degree) if specfied, then it means that freq is being specified in cycles/degree
  • meanlum - meanlum (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

hypergrat(s, freq, phase_deg, ori_deg, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 

2D hyperbolic (non-Cartesian) grating generator.

NB frequencies are in cycles/sprite or cycles/360deg

NB verified frequencies are really cycles/sprite JM 17-sep-2006

Parameters:
  • s - (Sprite) target sprite
  • freq - frequency (cycles/sprite or cyc/deg - see ppd)
  • phase_deg - (degrees)
  • ori_deg - (degrees) orientation
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/deg) if specified, then it means that freq is being specified in cycles/degreee
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

hartley(s, kx, ky, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, color=None)

source code 
Hartley basis function generator (after Ringach et al, 1997)
Parameters:
  • s - (Sprite) target sprite
  • kx, ky - combind ori/sf/phase params (see paper) these are basically in cycles/sprite, so the nyquist limit is size/2..
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

gabor(s, frequency, phase_deg, ori_deg, sigma, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, ppd=None, color=None)

source code 
2D gabor generator.
Parameters:
  • s - (Sprite) target sprite
  • frequency - frequency in cycles/sprite (or cyc/deg, if ppd is given)
  • phase_deg - (degrees) (nb: 0deg phase centers the sine function at sprite ctr)
  • ori_deg - (degrees) grating orientation
  • sigma - (pixels) envelope width -- sprite should be at least 6*sigma by 6*sigma in width and height
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • ppd - (pixels/degree) if specified, then it means that freq is being specified in cycles/degree
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

uniformnoise(s, binary=False, R=1.0, G=1.0, B=1.0, meanlum=0.5, moddepth=1.0, color=None)

source code 
Fill sprite with uniform white noise
Parameters:
  • s - (Sprite) target sprite
  • binary - (boolean) binary noise? (default=False)
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • moddepth - modulation depth (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

gaussiannoise(s, R=1.0, G=1.0, B=1.0, meanlum=0.5, stddev=1.0, color=None)

source code 
Fill sprite with Gaussian white noise
Parameters:
  • s - (Sprite) target sprite
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
  • meanlum - mean (DC) value of grating (0-1); default is 0.5
  • stddev - std of gaussian distribution (0-1)
  • color - RGB triple (alternative specification of color vector)
Returns:
nothing (works in place)

alphabar(s, bw, bh, ori_deg, R=1.0, G=1.0, B=1.0)

source code 

Generate a bar into existing sprite using the alpha channel.

This fills the sprite with 'color' and then puts a [bw x bh] transparent bar of the specified orientation in the alpha channel.

Parameters:
  • s - Sprite()
  • bw, bh - (pixels) bar width and height
  • ori_deg - (degrees) bar orientation
  • R, G, B - (either R is colortriple or R,G,B are 0-1 values)
Returns:
nothing (works in place)

alpha_gaussian(s, xsigma, ysigma=None, ori_deg=0.0)

source code 

Generate symmetric and asymmetric Gaussian envelopes into the alpha channel.

NB alpha's have peak value of fully visible (255), low end depends on sigma

Parameters:
  • s - (Sprite)
  • xsigma - (pixels) standard dev (think of this as the Gaussian's generated with ori=0 and then rotated)
  • ysigma - (pixels) if None, then use xsigma (symmatric)
  • ori_deg - (degrees) orientation of Gaussian
Returns:
nothing (works in place)

alphaGaussian(s, xsigma, ysigma=None, ori_deg=0.0)

source code 

Generate symmetric and asymmetric Gaussian envelopes into the alpha channel.

NB alpha's have peak value of fully visible (255), low end depends on sigma

Parameters:
  • s - (Sprite)
  • xsigma - (pixels) standard dev (think of this as the Gaussian's generated with ori=0 and then rotated)
  • ysigma - (pixels) if None, then use xsigma (symmatric)
  • ori_deg - (degrees) orientation of Gaussian
Returns:
nothing (works in place)