Friday, June 4, 2010

Data visualization in Python using Scipy


http://www.scipy.org/doc/api_docs/SciPy.misc.pilutil.html#toimage

If you have numpy and scipy available (and if you are manipulating large arrays in Python, I would recommend them), then the scipy.misc.pilutil.toimage function is very handy. A simple example:

import numpy as np

import scipy.misc.pilutil as smp

# Create a 1024x1024x3 array of 8 bit unsigned integers

data = np.zeros( (1024,1024,3), dtype=np.uint8 )

data[512,512] = [254,0,0]       # Makes the middle pixel red

data[512,513] = [0,0,255]       # Makes the next pixel blue

img = smp.toimage( data )       # Create a PIL image

img.show()                      # View in default viewer


The nice thing is toimage copes with diferent data types very well, so a 2d array of floating point numbers gets sensibly converted to greyscale etc.

to save the image to file, the function smp.imsave(filename, data) could be directly used.

Also there are other kind of methods: http://stackoverflow.com/questions/434583/what-is-the-fastest-way-to-draw-an-image-from-discrete-pixel-values-in-python

An example image:


Posted via email from Troy's posterous

No comments:

Post a Comment

Google+