如何改变 pyglet 的颜色 window
how to change the color of a pyglet window
我正在创建一个程序,它必须更改 pyglet window 中各个像素的颜色。我无法在文档中找到任何方法来执行此操作。有办法吗?
您可以将像素位移到背景中 'image'。您可以查看 this Stack Overflow 问题。
你可以使用神奇的功能SolidColorImagePattern
,修改你需要的数据。
R,G,B,A = 255,255,255,255
pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(width,height)
这是一张 .blit()
:可用的图像。它是白色的,可能不是你想要的。
所以我们会做一些更神奇的事情,将所有像素换成随机像素(War 的蚂蚁):
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()
出于教育目的,我会将其分解为更简单的部分。
image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)
如前所述,创建纯白色图像。它的宽度和高度与 window 大小相匹配。
然后我们抓取图像数据:
data = image.get_image_data().get_data('RGB', width*3)
此 bytes
字符串将包含 width*height*<format>
,这意味着 20x20
图像将 1200
字节大,因为 RGB 每个像素占用 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
整个块循环遍历所有像素(len(data) 只是一个方便的东西,你也可以 range(0, width*height*3, 3)
,但是嗯。
像素由 3 个 randint(255) 字节对象组合成一个字符串,如下所示:
pixel = b'xffxffxff'
这也是我们 3
进入 range(0, len(data), 3)
的原因。因为一个像素是3个字节"wide".
一旦我们生成了所有像素(出于某种原因,无法修改字节对象 image
..我可以发誓我之前修改过字节 "strings"..我'我累了,所以这可能是一个乌托邦式的梦想之类的。
无论如何,一旦完成所有可爱的图像构建,我们就可以通过以下方式为图像对象提供新数据:
image.set_data('RGB', width*3, new_image)
就是这样。在 -45 度的冬日里,在阳光下像黄油一样轻松。
文档:
- https://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/programming_guide/quickstart.html
- https://github.com/Torxed/PygletGui/blob/master/gui_classes_generic.py
- https://pythonhosted.org/pyglet/api/pyglet.image.ImageData-class.html#get_image_data
- https://pythonhosted.org/pyglet/api/pyglet.image.ImageData-class.html#set_data
您也可以选择加入一个区域,然后修改一个区域。但我会把修补工作留给您:)
为了好玩,我将添加另一个更符合您可能需要的答案。因为 window 本身将是您通过以下方式决定的任何 "clear" 颜色缓冲区:
window = pyglet.window.Window(width=width, height=height)
pyglet.gl.glClearColor(0.5,0,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).
因此更改背景几乎是不可能的,因为它是 "nothing"。
但是,您可以通过 .draw()
函数在背景上绘制像素。
import pyglet
from random import randint
width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)
@window.event
def on_draw():
window.clear()
for i in range(10):
x = randint(0,width)
y = randint(0,height)
pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
('v2i', (x, y)),
('c3B', (255, 255, 255))
)
pyglet.app.run()
这将在背景上创建 10 个随机放置的白点。
要在上面添加任何内容,只需将 .blit()
或 .draw()
功能放在 pyglet.graphics.draw()
行之后。
如果你指的是背景色,我可以帮忙。我知道一种选择,pyglet.gl.glClearColor 函数。
例如:
import pyglet
from pyglet.gl import glClearColor
win = pyglet.window.Window(600, 600, caption = "test")
glClearColor(255, 255, 255, 1.0) # red, green, blue, and alpha(transparency)
def on_draw():
win.clear()
这将创建一个 window 具有白色背景(而不是默认的黑色)
我正在创建一个程序,它必须更改 pyglet window 中各个像素的颜色。我无法在文档中找到任何方法来执行此操作。有办法吗?
您可以将像素位移到背景中 'image'。您可以查看 this Stack Overflow 问题。
你可以使用神奇的功能SolidColorImagePattern
,修改你需要的数据。
R,G,B,A = 255,255,255,255
pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(width,height)
这是一张 .blit()
:可用的图像。它是白色的,可能不是你想要的。
所以我们会做一些更神奇的事情,将所有像素换成随机像素(War 的蚂蚁):
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()
出于教育目的,我会将其分解为更简单的部分。
image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)
如前所述,创建纯白色图像。它的宽度和高度与 window 大小相匹配。
然后我们抓取图像数据:
data = image.get_image_data().get_data('RGB', width*3)
此 bytes
字符串将包含 width*height*<format>
,这意味着 20x20
图像将 1200
字节大,因为 RGB 每个像素占用 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
整个块循环遍历所有像素(len(data) 只是一个方便的东西,你也可以 range(0, width*height*3, 3)
,但是嗯。
像素由 3 个 randint(255) 字节对象组合成一个字符串,如下所示:
pixel = b'xffxffxff'
这也是我们 3
进入 range(0, len(data), 3)
的原因。因为一个像素是3个字节"wide".
一旦我们生成了所有像素(出于某种原因,无法修改字节对象 image
..我可以发誓我之前修改过字节 "strings"..我'我累了,所以这可能是一个乌托邦式的梦想之类的。
无论如何,一旦完成所有可爱的图像构建,我们就可以通过以下方式为图像对象提供新数据:
image.set_data('RGB', width*3, new_image)
就是这样。在 -45 度的冬日里,在阳光下像黄油一样轻松。
文档:
- https://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/programming_guide/quickstart.html
- https://github.com/Torxed/PygletGui/blob/master/gui_classes_generic.py
- https://pythonhosted.org/pyglet/api/pyglet.image.ImageData-class.html#get_image_data
- https://pythonhosted.org/pyglet/api/pyglet.image.ImageData-class.html#set_data
您也可以选择加入一个区域,然后修改一个区域。但我会把修补工作留给您:)
为了好玩,我将添加另一个更符合您可能需要的答案。因为 window 本身将是您通过以下方式决定的任何 "clear" 颜色缓冲区:
window = pyglet.window.Window(width=width, height=height)
pyglet.gl.glClearColor(0.5,0,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).
因此更改背景几乎是不可能的,因为它是 "nothing"。
但是,您可以通过 .draw()
函数在背景上绘制像素。
import pyglet
from random import randint
width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)
@window.event
def on_draw():
window.clear()
for i in range(10):
x = randint(0,width)
y = randint(0,height)
pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
('v2i', (x, y)),
('c3B', (255, 255, 255))
)
pyglet.app.run()
这将在背景上创建 10 个随机放置的白点。
要在上面添加任何内容,只需将 .blit()
或 .draw()
功能放在 pyglet.graphics.draw()
行之后。
如果你指的是背景色,我可以帮忙。我知道一种选择,pyglet.gl.glClearColor 函数。
例如:
import pyglet
from pyglet.gl import glClearColor
win = pyglet.window.Window(600, 600, caption = "test")
glClearColor(255, 255, 255, 1.0) # red, green, blue, and alpha(transparency)
def on_draw():
win.clear()
这将创建一个 window 具有白色背景(而不是默认的黑色)