5

Instead of using directories to reference an image, is it possible to code an image into the program directly?

8
  • 1
    What do you mean by code an image into a program directly ? You mean having something like an image constant. Commented 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. Commented 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. Commented 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. Commented Sep 15, 2010 at 16:44
  • 1
    These files are special because they aren't delivered as part of the python library, obviously. Commented Sep 26, 2015 at 3:33

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

For the 'data to be encoded', how would I specify the image I want? I can't put the directory since that leads to it encoding the directory. How do I encode the image?
You would need to open the file, read its contents and pass that to b64encode. f = open('image.png'); base64.b64encode(f.read())
But, I don't really understand it, from my understanding, the point of this question is to get rid of the dependence on external files, but by doing this you would need to call the image file everytime you run the code, wouldn't you?
No Lpng. You run it once to create the encoded string. Save that to the target file, along with the decode line.
5

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

It seems to use base64, so the generated Python code can with small modifications (using Arlaharens answer) be used without adding a dependency on wxpython.
1

There is no need to base64 encode the string, just paste it's repr into the code

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.