Skip to content

Instantly share code, notes, and snippets.

@ahinz
Last active December 22, 2015 15:49
Show Gist options
  • Save ahinz/6495407 to your computer and use it in GitHub Desktop.
Save ahinz/6495407 to your computer and use it in GitHub Desktop.
# apt-get install apt-get install libblas-dev liblapack-dev gfortran
# pip install numpy
# pip install scipy pil
from math import exp, sqrt
from scipy import array, zeros
from scipy.signal import fftconvolve
from scipy.misc import imsave
from random import randint
def gaussian(r, a=1.0, b=0, c=1.0):
return a*exp(-((r - b) * (r - b) / (2 * c * c)))
def gaussian2d(w, h, a=1.0, b=0, c=1.0):
arr = zeros((w,h), dtype=float)
for x in xrange(0, w):
xf = float(x) - w/2.0
for y in xrange(0, h):
yf = float(y) - h/2.0
r = sqrt(xf*xf + yf*yf)
arr[x,y] = gaussian(r, a, b, c)
return arr
def burnPointsToArray(pts, arr):
for x, y in pts:
arr[x,y] += 1
return arr
def createRandomPoints(xmax, ymax, n):
r = lambda: (randint(0, xmax-1), randint(0, ymax-1))
return [r() for _ in xrange(n)]
def createRandomPointRaster(width=700, height=700, n=200):
arr = zeros((width, height), dtype=float)
pts = createRandomPoints(width, height, n)
return burnPointsToArray(pts, arr)
rando = createRandomPointRaster()
kern = gaussian2d(100, 100, c=25.0)
done = fftconvolve(rando, kern, 'same')
imsave('outfile.jpg', done)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment