putpec with pyglet [英] putpixel with pyglet
问题描述
我是pyglet的新手。我想在每个 on_draw
迭代中将像素从黑色更改为白色。因此,在1000次迭代之后,窗口中应该只有1000个白色像素。但是,我想避免在 on_draw
中调用1000个绘制操作。所以我想创建一个图像,在图像上做一个RGB putpixel,然后将图像blit到屏幕上。我怎样才能做到这一点? pyglet文档,示例和源代码对此没有太大帮助。
I'm new to pyglet. I'd like to change a pixel from black to white at each on_draw
iteration. So after 1000 iterations, there should be exactly 1000 white pixels in the window. However, I'd like to avoid calling 1000 draw operations in on_draw
for that. So I'd like to create an image, do an RGB putpixel on the image, and blit the image to the screen. How can I do that? The pyglet documentation, the examples and the source code aren't too helpful on this.
推荐答案
因为没有人真的很好回答这个。
我会在这里放一个:
Since no one gave a really good answer to this.
I'll place one here:
import pyglet
from random import randint
width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)
image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)
data = image.get_image_data().get_data('RGB', width*3)
new_image = b''
for i in range(0, len(data), 3):
pixel = bytes([randint(0,255)]) + bytes([randint(0,255)]) + bytes([randint(0,255)])
new_image += pixel
image.set_data('RGB', width*3, new_image)
@window.event
def on_draw():
window.clear()
image.blit(0, 0)
pyglet.app.run()
基本上,这就是它的作用吃了一张白色的图像,它本身可以在窗口中绘制。但看到我们想做 putpixel
,我已经修改了白色图像中的每个像素作为演示。
Essentially, what this does is it creates a white image, that in itself can be drawn in the window. But seeing as we want to do putpixel
, i've modified every pixel in the white image as a demo.
可与以下交界处使用:
- https://pythonhosted.org/pyglet/api/pyglet.image.ImageData-class.html#get_region
可以用来进一步确定图像处理。
Which can be used to optemize the image manipulation further.
这篇关于putpec with pyglet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!