在 pyglet 中加载枕头图像作为纹理

问题描述 投票:0回答:1

问题

我正在尝试使用以下代码将 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 中工作。

python python-imaging-library pyglet
1个回答
0
投票

我尝试使用您的示例,它似乎在 Windows 10 上运行良好且没有错误。Pyglet 2.0.5.

之后我得到这个结果然后保存它:

© www.soinside.com 2019 - 2024. All rights reserved.