Guest User

Untitled

a guest
Oct 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ''' Quick 3D SPLEEM visualization script. Requires mayavi '''
  3.  
  4. import sys
  5. import pylab as pl
  6. from enthought.mayavi import mlab
  7. import Image
  8.  
  9. w = 100
  10. X, Y = pl.meshgrid(range(-w/2, w/2), range(-w/2, w/2))
  11. R = pl.sqrt(X**2+Y**2)
  12.  
  13. def im2arr(path, resize=False):
  14.     ''' Loads an image as a 2d array, resized to a square, w by w '''
  15.     image = Image.open(path)
  16.     image = image.convert("I")
  17.     if resize:
  18.         image = image.resize(resize)
  19.     newArr = pl.fromstring(image.tostring(), pl.uint8).astype(pl.int16)
  20.     newArr = pl.reshape(newArr, (100, 100))
  21.     return newArr
  22.  
  23. def load_spleem(up_path, dn_path):
  24.     ''' Loads spleem data from two leem images '''
  25.     up = im2arr(up_path, resize=(w, w))
  26.     down = im2arr(dn_path, resize=(w, w))
  27.     spleem = (up-down) / (up+down)
  28.     return spleem
  29.  
  30. def touchup(im):
  31.     ''' removes integer offset (naiive interpretation,
  32.    subtracts mean) and crops to circle '''
  33.     image = im - im[pl.where(im != im.max())].mean()
  34.     image[pl.where(R>(w/2-5))] = 0
  35.     return image
  36.  
  37. def loadimages(p):
  38.     ''' Loads an arbitrary number of images,
  39.    touches them up and returns them as a tuple '''
  40.     ret = []
  41.     for path in p: ret.append(touchup(im2arr(path, resize=(w, w))))
  42.     return tuple(ret)
  43.  
  44. def loadspleems(p):
  45.     ''' Load several spleems at once '''
  46.     ret = []
  47.     for i in range(0, len(p), 2): ret.append(load_spleem(p[i], p[i+1]))
  48.     return tuple(ret)
  49.  
  50. if __name__ == '__main__':
  51.     x, y, z = (False, False, False)
  52.  
  53.     if len(sys.argv) > 1: f = sys.argv[1:]
  54.     else: f = raw_input('Which files would you like to evaluate?: ').split()
  55.  
  56.     if len(f) == 3: x, y, z = loadimages(f)
  57.     elif len(f) == 6: x, y, z = loadspleems(f)
  58.  
  59.     good = x.any() and y.any() and z.any()
  60.     assert good, 'Images have not been initialized.'
  61.     if not good: sys.exit(1)
  62.  
  63.     #z *= 0.2 # Scale z coordinate differently to enhance in-plane contrast.
  64.     mlab.figure(bgcolor=(1, 1, 1), size=(600, 400))
  65.     y = -1 * y
  66.     pts = mlab.quiver3d(x, y, z, scalars=y, colormap='RdYlBu', mode='cone', resolution=60, mask_points=3)
  67.     pts.glyph.color_mode = 'color_by_scalar'
  68.     mlab.view(*(95, 53, 105, pl.array([50, 70, 0])))
  69.     mlab.savefig('test_output.png', magnification=3)
  70.     #mlab.show()
  71.     #while True:
  72.     #    print mlab.veiw()
  73.     #mlab.savefig('snapshot.png', size=(2400, 1600))
Add Comment
Please, Sign In to add comment