in Education by
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 tried 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 "", line 245, in run_nodebug File "C:\Python26\GUI1.2.9.py", line 473, in 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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I'm not sure I understand 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 its contents - you cannot simply 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()

Related questions

0 votes
    Which class implements an encoder for encoding byte data using the Base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you create a Base64 encoder that encodes using the URL and Filename safe type base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you create a Base64 decoder that decodes using the URL and Filename safe type base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you create a Base64 encoder that encodes using the MIME type base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you create a Base64 decoder that decodes using the MIME type base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Which class implements a decoder for decoding byte data using the Base64 encoding scheme in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    I was trying to write a quick-and-dirty script to generate plots easily. For that, I was using the following code ( ... do I do that? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have an image with horizontal and vertical lines. In fact, this image is the BBC website converted to ... rectangle. Thanks! Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    When I try t display the Plotly graph from python as an image that works fine import plotly.express as px from IPython. ... ='png')))"] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How can I remove emojis that start with '\x' when reading a csv file using pandas in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want to upload the file pdf to the folder but a file is not saved to a folder. I already read the ... .run(debug=True)``` Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How to load a model from an HDF5 file in Keras? What I tried: model = Sequential() model.add(Dense(64, ... list index out of range Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm trying to import an XML file via a web page in a Ruby on Rails application, the code ruby ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I'm curious about manipulating time in Python. I can get the (last modified) age of a file using ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to solve the following problem: Write aprogram that reads the random numbers from the file ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
...