views:

190

answers:

5

I have created an array thusly:

import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]

What I want this to do is display a single red dot in the center of a 512x512 image. (At least to begin with... I think I can figure out the rest from there)

A: 

Using pygame, you can open a window, get the surface as an array of pixels, and manipulate as you want from there. You'll need to copy your numpy array into the surface array, however, which will be much slower than doing actual graphics operations on the pygame surfaces themselves.

Stefan Kendall
A: 

The Python Imaging Library can display images using Numpy arrays. Take a look at this page for sample code:

EDIT: As the note on the bottom of that page says, you should check the latest release notes which make this much simpler:

http://effbot.org/zone/pil-changes-116.htm

ars
A: 

Shortest path is to use scipy, like this:

from scipy.misc import toimage
toimage(data).show()

This requires PIL to be installed as well.

Peter Hansen
A: 

Do you just mean this?

from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()
Steve
+1  A: 

You could use PIL to create a still image:

import Image
import numpy as np
w,h = 512,512
data = np.zeros( (w,h,3), dtype=np.uint8)
data[256,256] = [255,0,0]
img = Image.fromarray(data, 'RGB')
img.save('my.png')
unutbu
This works beautifully. Thank you.
jlswint