tags:

views:

121

answers:

3

I want to encode an image into a string using the base64 module. I've ran into a problem though. How do I specify the image I want to be encoded? I tried using the directory to the image, but that simply leads to the directory being encoded. I want the actual image file to be encoded.

EDIT

I tired this

snippet:

with open("C:\Python26\seriph1.BMP", "rb") as f:
            data12 = f.read()
        UU = data12.encode("base64")
        UUU = base64.b64decode(UU)

        print UUU





        self.image = ImageTk.PhotoImage(Image.open(UUU))

but I get the following error:

Traceback (most recent call last):
  File "<string>", line 245, in run_nodebug
  File "C:\Python26\GUI1.2.9.py", line 473, in <module>
    app = simpleapp_tk(None)
  File "C:\Python26\GUI1.2.9.py", line 14, in __init__
    self.initialize()
  File "C:\Python26\GUI1.2.9.py", line 431, in initialize
    self.image = ImageTk.PhotoImage(Image.open(UUU))
  File "C:\Python26\lib\site-packages\PIL\Image.py", line 1952, in open
    fp = __builtin__.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

What am I doing wrong?

+5  A: 

I'm not sure I get your question. I assume you are doing something along the lines of:

import base64

with open("yourfile.ext", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

You have to open the file first of course, and read it's contents - you cannot simple pass the path to the encode function.

Edit: Ok, here is an update after you have edited your original question.

First of all, remember to use raw strings (prefix the string with 'r') when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL's Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).

That being said, you can use cStringIO to create such an object from a memory buffer:

import cStringIO
import PIL.Image

# assume data contains your decoded image
file_like = cStringIO.StringIO(data)

img = PIL.Image.open(file_like)
img.show()
Jim Brissom
Need a bit more help, see edit.
Anteater7171
Thanks, one more problem when I print the decoded image I get the string 'ÿØÿà'. However, when I run this alone as a substitute for data I get an error. The encoded string is much longer for comparison. So I think that likely stores the image data. does the decoded string simply reference the encoded string or something? It seems far too short for data storage.
Anteater7171
The printed output is not necessarily equal to the actual contents - it depends on how and where you print it.
Jim Brissom
+1  A: 

As I said in your previous question, there is no need to base64 encode the string, it will only make the program slower. Just use the repr

>>> with open("images/image.gif", "rb") as fin:
...  image_data=fin.read()
...
>>> with open("image.py","wb") as fout:
...  fout.write("image_data="+repr(image_data))
...

Now the image is stored as a variable called image_data in a file called image.py Start a fresh interpreter and import the image_data

>>> from image import image_data
>>>
gnibbler
I don't really see how repr() can be of any use here.
Ivo van der Wijk
@Ivo, Anteater wants to be able to store images in python files. I am pointing out that using base64 is counterproductive because the data needs to be decoded every time the module is loaded. Using repr instead means the literal string is stored ready for immediate use in the .pyc file with no furthur processing
gnibbler
+1  A: 

With python 2.x, you can trivially encode using .encode:

with open("path/to/file.png", "rb") as f:
    data = f.read()
    print data.encode("base64")
Ivo van der Wijk