Tireless22
Tireless22

Reputation: 31

Encode and decode .gif file to be used with pyglet and made into .exe with pyinstaller

I want to bake a .gif file into .exe with pyinstaller. From my research I have to encode the gif to a string and decode that string for this to be possible. Encoding the string and decoding the string is working. I get an error when trying to use it as a animation resource in pyglet. Something in the encoding-decoding has broken the gif? If there is another preferred way of doing this please let me know!

Step1

import base64

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

Step2

Saving this string in a .py file like this:

image = b'AWggsegsegs/....'

Step3

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)

Error message reads

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.

Upvotes: 3

Views: 825

Answers (1)

hidefromkgb
hidefromkgb

Reputation: 5903

Instead of pyglet.resource.animation() which can only load files, you should use pyglet.image.load_animation(). Its optional File parameter supports file-like objects, so you should construct one using io.BytesIO() and pass it there.

Upvotes: 1

Related Questions