我想用pyinstaller将.gif
文件烘焙到.exe
中。根据我的研究,我必须将gif编码为字符串并解码该字符串以实现此目的。编码字符串并解码字符串是有效的。尝试将其用作pyglet中的动画资源时出错。编码解码中的东西打破了gif?如果有另一种首选方式,请告诉我!
步骤1
import base64
with open("test.gif", "rb") as imageFile:
image = base64.b64encode(imageFile.read())
print(image)
第2步
将此字符串保存在.py文件中,如下所示:
image = b'AWggsegsegs/....'
第三步:
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()
构造一个并将其传递给那里。