Instead of using directories to reference an image, is it possible to code an image into the program directly?
You can use the base64 module to embed data into your programs. From the base64 documentation:
>>> import base64
>>> encoded = base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
'data to be encoded'
Using this ability you can base64 encode an image and embed the resulting string in your program. To get the original image data you would pass that string to base64.b64decode
.
If you mean, storing the bytes that represent the image in the program code itself, you could do it by base64 encoding the image file, and setting a variable to that string.
You could also declare a byte array, where the contents of the array are the bytes that represent the image.
In both cases, if you want to operate on the image, you may need to decode the value that you have included in your source code.
Warning: you may be treading on a performance minefield here.
A better way might be to store the image/s in the directory structure of your module, and the loading it on demand (even caching it). You could write a generalized method/function that loads the right image based on some identifier which maps to the particular image file name that is part and parcel of your module.
In Java or C# you can put the image file content in an String by using a base64 encoding, then this String is put in the source code as a constant.
The Program use the String decode it to some kind of byte array or stream and convert the byte array/stream to an image.
I belive you can do the same with python.
There is no need to base64 encode the string, just paste it's repr
into the code
This utility at UtilityMill will take a file, zip it, and give you the base 64 encoded string. Paste this as a string right into your Python script. The utility page at the bottom gives you the code you'll need to reinflate the file in your Python script. The result will be the same as if you had read the contents of the file from disk.
Here is a link to an example showing a small image file converted to b64, stored in a string, and then the code to re-expand it.
Try img2py script. It's included as part of wxpython (google to see if you can dl seperately).
img2py.py -- Convert an image to PNG format and embed it in a Python module with appropriate code so it can be loaded into a program at runtime. The benefit is that since it is Python source code it can be delivered as a .pyc or 'compiled' into the program using freeze, py2exe, etc. Usage:
img2py.py [options] image_file python_file