I'd like to figure out how to embed binary content in a python script. For instance, I don't want to have any external files around (images, sound, ... ), I want all this content living inside of my python scripts.
Little example to clarify, let's say I got this small snippet:
from StringIO import StringIO
from PIL import Image, ImageFilter
embedded_resource = StringIO(open("Lenna.png", "rb").read())
im = Image.open(embedded_resource)
im_sharp = im.filter(ImageFilter.SHARPEN)
The best way to go about this is converting your picture into a python string, and have it in a separate file called something like
resources.py, then you simply parse it.
In the first scenario, you could even use
base64 to (de)code the picture, something like this:
import base64 file = open('yourImage.png'); encoded = base64.b64encode(file.read()) data = base64.b64decode(encoded) # Don't forget to file.close() !