Advertisement
Guest User

Numpy

a guest
Jun 29th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. #
  2. # Good god my code is shite
  3. # Kind Regards
  4.  
  5. import numpy as np
  6. import math
  7. import Image
  8.  
  9. def dofilter(array1):
  10.     xsize, ysize = array1.shape
  11.     a0 = np.zeros(size)
  12.     diffx = np.diff(array1)
  13.     diffy = np.diff(array1,axis=0)
  14.     a0[:,:xsize-1] += diffx
  15.     a0[:,1:] -= diffx
  16.     a0[:ysize-1] += diffy
  17.     a0[1:] -= diffy
  18.     return a0*0.5
  19.  
  20. def step_forward(a1,a1v,n=1,action=None):
  21.     for bx in xrange(n):
  22.         if action:
  23.             a1 = action(a1)
  24.         a1v += dofilter(a1)*k_const
  25.         a1 += a1v
  26.     return a1, a1v
  27.  
  28. def action_1(x):
  29.     x[ox-1:ox+1,oy-1:oy+1] = 0.9
  30.     return x
  31.  
  32.  
  33. size = (200,200)
  34. k_const = 0.25
  35. speed = 3
  36. a1 = np.zeros(size)
  37. a1v = np.zeros(size)
  38.  
  39.  
  40. for imnum in xrange(400):
  41.     ox = int(100 + (math.sin(imnum*0.2)*20))
  42.     oy = int(100 - (math.cos(imnum*0.2)*20))
  43.    
  44.     if imnum < 75:
  45.         a1, a1v = step_forward(a1,a1v,n=speed,action=action_1)
  46.     else:
  47.         a1, a1v = step_forward(a1,a1v,n=speed)
  48.  
  49.     im = Image.fromarray(np.uint8((a1+1)*128))
  50.     im.save("output_k_" + format(imnum,"03d") + ".png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement