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()
Answer from Jim Brissom on Stack OverflowI'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()
import base64
from PIL import Image
from io import BytesIO
with open("image.jpg", "rb") as image_file:
data = base64.b64encode(image_file.read())
im = Image.open(BytesIO(base64.b64decode(data)))
im.save('image1.png', 'PNG')
Base 64 Encode Python Files!
Python 3 and base64 encoding of a binary file - Stack Overflow
Stupid Linux tricks - use base64 to perfectly preserve formatting when copy/pasting between terminals, ssh sessions, serial connections, etc.
Converting Base64 file back to Excel
import base64, os
path = ""
path2 = ""
flag = False
while not os.path.isfile(path):
if flag:
print("File path does not exist!\n")
path = input("Enter path to python file to base64 encode: ")
flag = True
path2 = input("Enter path to save base64 encoded python file: ")
if os.path.isfile(path):
contentb64 = ""
with open(path, "r") as file:
content = file.read()
contentb64 = base64.b64encode(content.encode()).decode()
contentb64 = 'import base64; exec(base64.b64decode(b"%s").decode())' % contentb64
with open(path2, "w") as file:
file.write(contentb64)
print("dOnE!")
input(); exit()Base 64 encodes python code, can be useful if you don't want people to see your code, just modify this one to do it multiple times as you wish.
From the base64 package doc:
base64.encodestring:"Encode the bytes-like object s, which can contain arbitrary binary data, and return
bytescontaining the base64-encoded data, with newlines (b"\n") inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME)."
You want to use
base64.b64encode:"Encode the bytes-like object s using Base64 and return the encoded
bytes."
Example:
import base64
with open("test.zip", "rb") as f:
encodedZip = base64.b64encode(f.read())
print(encodedZip.decode())
The decode() will convert the binary string to text.
Use b64encode to encode without the newlines and then decode the resulting binary string with .decode('ascii') to get a normal string.
encodedZip = base64.b64encode(zipContents).decode('ascii')