Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to code images into a python script?

Tags:

python

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

like image 996
rectangletangle Avatar asked Sep 15 '10 06:09

rectangletangle


2 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.

like image 57
Arlaharen Avatar answered Sep 25 '22 23:09

Arlaharen


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

like image 27
fseto Avatar answered Sep 22 '22 23:09

fseto