我想用pyinstaller将.gif文件烘焙到.exe中。根据我的研究,我必须将gif编码成一个字符串,并对该字符串进行解码,这样才有可能。正在对字符串进行编码和解码。在pyglet中尝试将其用作动画资源时出错。编码解码中有什么东西破坏了gif?如果有其他更好的方法,请让我知道!
步骤1

import base64

with open("test.gif", "rb") as imageFile:
    image = base64.b64encode(imageFile.read())
    print(image)

第二步
将此字符串保存在.py文件中,如下所示:
image = b'AWggsegsegs/....'

步骤3
import pyglet
import base64
from gif_string import image


decodedgif = base64.b64decode(image) # Works this far
animation = pyglet.resource.animation(decodedgif) # Error here
sprite = pyglet.sprite.Sprite(decodedgif)

读取错误消息
Traceback (most recent call last):
  File "...pyglet\resource.py", line 583, in animation
    identity = self._cached_animations[name]
  File "...AppData\Local\Programs\Python\Python36\lib\weakref.py", line 131, in __getitem__
    o = self.data[key]()
KeyError: b'GIF89a
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "...\pyglet\resource.py", line 435, in file
    location = self._index[name]
KeyError: b'GIF89a\
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\..., line 7, in <module>
    animation = pyglet.resource.animation(decodedgif) # Error here
  File "...\pyglet\resource.py", line 585, in animation
    animation = pyglet.image.load_animation(name, self.file(name))
  File "...\pyglet\resource.py", line 438, in file
    raise ResourceNotFoundException(name)
pyglet.resource.ResourceNotFoundException: Resource "b'GIF89a\x00\
...
...f9\x88\xc0\xbe\xa4O"o\xcf\xe5\x81\x00\x00;'" was not found on the path.
  Ensure that the filename has the correct captialisation.

最佳答案

您应该使用pyglet.resource.animation(),而不是只能加载文件的pyglet.image.load_animation()。它的可选File参数支持file-like objects,因此您应该使用io.BytesIO()构造一个并将其传递到那里。

关于python - 编码和解码要与pyglet一起使用的.gif文件,并通过pyinstaller制成.exe,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55614943/

10-12 21:27