I am having problems displaying a numpy array with pyglet. I found a very similar topic ( how to display a numpy array with pyglet? ) That I used. I want to display the array in shades of gray, but pyglet displays it with colors to see the image: http://i.stack.imgur.com/pL6Yr.jpg
def create(self, X,Y): IMG = random((X,Y)) * 255 self.IMG = dstack((IMG,IMG,IMG)) return self.IMG def image(self): self.img_data = self.create(X,Y).data.__str__() self.image = pyglet.image.ImageData(X,Y, 'RGB', self.img_data, pitch = -X*3) return self.image
If I save and load the array, then it works (but it is terribly slower):
def image(self): self.im_save=scipy.misc.toimage(self.create(X,Y),cmin=0, cmax=255) self.im_save.save('outfile.png') self.image = pyglet.image.load('outfile.png') return self.image
And I get what I want:
i.stack.imgur.com/FCY1v.jpg
I can not find the error in the first code example :(
EDIT:
Thanks so much for your answers. With a tip from Bago, I got this to make the code work :) And really, the nfirvine suggestion is reasonable, since I want to display only the matrix in shades of gray.
def create(self, X,Y): self.IMG = (random((X,Y)) * 255).astype('uint8') return self.IMG def image(self): self.img_data = self.create(X,Y).data.__str__() self.image = pyglet.image.ImageData(X,Y, 'L', self.img_data) return self.image