问题描述
我在用pyglet显示numpy数组时遇到问题.我发现了一个非常相似的主题(如何使用pyglet显示numpy数组?).我想以灰度显示数组,但是pyglet用彩色显示它,请参见图像: http://i.stack.imgur.com/pL6Yr.jpg
I have problems with displaying a numpy array with pyglet. I have found a very similar topic (how to display a numpy array with pyglet?) that I used. I want to display the array in greyscale, but pyglet displays it with colours 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 instead it works (but it is horrobly 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 wanted:
i.stack.imgur.com/FCY1v.jpg
i.stack.imgur.com/FCY1v.jpg
在第一个代码示例中我找不到错误:(
I can't find the mistake in the first code example :(
非常感谢您的回答.有了Bago的提示,我就可以开始编写代码了:)实际上,nfirvine的建议是合理的,因为我只想以灰度显示矩阵.
Many thanks for your answers. With the hint from Bago I got this to code to work :) And indeed nfirvine suggestion is reasonable, since I only want to display the matrix in greyscale.
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
推荐答案
我认为pyglet期望使用uint8,您尝试过吗?
I think pyglet is expecting uint8, have you tried?
IMG = ( random((X,Y)) * 255 ).astype('uint8')
这篇关于numpy数组显示与pyglet不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!