我正在尝试使用以下代码将 Pillow 图像加载为 Pyglet 纹理:
pixels = pillow_image.convert("L").tobytes()
width, height = pillow_image.size
image = pyglet.image.ImageData(width, height, "L", pixels, pitch=width)
image = image.get_texture().get_transform(flip_y=True)
它给了我以下错误信息:
image = image.get_texture().get_transform(flip_y=True)
File "/home/home_dir/.local/share/virtualenvs//app/-r0DkEahp/lib/python3.8/site-packages/pyglet/image/__init__.py", line 692, in get_texture
self._current_texture = self.create_texture(Texture, rectangle)
File "/home/home_dir/.local/share/virtualenvs//app/-r0DkEahp/lib/python3.8/site-packages/pyglet/image/__init__.py", line 681, in create_texture
texture = cls.create(self.width, self.height, GL_TEXTURE_2D, internalformat)
File "/home/home_dir/.local/share/virtualenvs//app/-r0DkEahp/lib/python3.8/site-packages/pyglet/image/__init__.py", line 1260, in create
glTexImage2D(target, 0,
File "/home/home_dir/.local/share/virtualenvs//app/-r0DkEahp/lib/python3.8/site-packages/pyglet/gl/lib.py", line 79, in errcheck
raise GLException(f'(0x{error}): {msg}')
pyglet.gl.lib.GLException: (0x1281): Invalid value. A numeric argument is out of range.
我尝试调整图像大小,因为我读到纹理有大小限制,但没有运气。
考虑下图:
下载此图片并将其命名为“gravatar.png”。然后下面的代码用最新的 pyglet 版本重现了这个问题:
import pyglet
from PIL import Image
im = Image.open("/path/to/gravatar.png").convert("L")
width, height = im.size
image = pyglet.image.ImageData(width, height, "L", im.tobytes(), pitch=width)
image = image.get_texture().get_transform(flip_y=True)
我意识到我在此处发布的代码适用于 1.5.x 版本的 pyglet,但不适用于 2.x 版本的 pyglet。这解释了为什么它以前有效,因为我最初是在第一个 2.x 版本之前编写代码的。
鉴于它适用于 1.5.x 版本的 pyglet,我现在的问题是如何让它在 2.x 版本的 pyglet 中工作。