Instead of using directories to reference an image, is it possible to code an image into the program directly?
-
1What do you mean by code an image into a program directly ? You mean having something like an image constant.Rahul– Rahul2010-09-15 06:42:53 +00:00Commented Sep 15, 2010 at 6:42
-
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.Ralph– Ralph2010-09-15 06:58:29 +00:00Commented Sep 15, 2010 at 6:58
-
Convert the image to a string (or any other data structure). So that I would not have to use the images directory.rectangletangle– rectangletangle2010-09-15 07:01:16 +00:00Commented Sep 15, 2010 at 7:01
-
2@S.Lott - The images are pretty small, They usually translate out into sub 5 lines of code. Also the reason is to entirely remove my scripts dependency on external files.rectangletangle– rectangletangle2010-09-15 16:44:31 +00:00Commented Sep 15, 2010 at 16:44
-
1These files are special because they aren't delivered as part of the python library, obviously.GreenAsJade– GreenAsJade2015-09-26 03:33:49 +00:00Commented Sep 26, 2015 at 3:33
4 Answers
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.
4 Comments
encoded string. Save that to the target file, along with the decode line.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
1 Comment
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.