Cant really put my finger on what you are doing wrong, but the code seems to be more complex then it has to be.
Instead of doing the read operations, let base64 module handle it for you
import base64
with open('noaa19-161015.png', 'rb') as fp, open('test.b64', 'w') as fp2:
base64.encode(fp, fp2)
with open('test.b64', 'rb') as fp, open('test.png', 'wb') as fp2:
base64.decode(fp, fp2)
Also noticed that you are trying to encode a already encoded string
look at the row base64.b64encode(string)
Cant really put my finger on what you are doing wrong, but the code seems to be more complex then it has to be.
Instead of doing the read operations, let base64 module handle it for you
import base64
with open('noaa19-161015.png', 'rb') as fp, open('test.b64', 'w') as fp2:
base64.encode(fp, fp2)
with open('test.b64', 'rb') as fp, open('test.png', 'wb') as fp2:
base64.decode(fp, fp2)
Also noticed that you are trying to encode a already encoded string
look at the row base64.b64encode(string)
while you're opening a not txt-like file, using 'rb' will avoid that error.
python - Encoding an image file with base64 - Stack Overflow
How to decode base64 text file into .png
python - How do you base-64 encode a PNG image for use in a data-uri in a CSS file? - Stack Overflow
Encode png bytes to base64
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()
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')
Data in the text file is base64 png image. I am trying to decode data file and save to data.png . PyCharm is not showing error in code but when i tried to open image it says: the file "something.png" could not be opened. It may be damaged or use a file format that preview doesn't recognize.
Please advise.
ovo.txt: iw0KGgoVBORAAAANSUhEUgAAADwAAAAeCAIAAAD/+uoYAAAAO0lEQVR4nO3OwQ0AEAAAMey/Mzt4VCTXCTr3+M96HbhRWimtlFZKK6WV0kpppbRSWimtlFZKK6WV0soBsq4BO2SEu+wAAAAASUVORK5CYII=
Code:
import base64
data = open("ovo.txt", "r").read()
decoded = base64.b64decode(data)
filename = "something.png"
with open("something.png", "w") as f:
f.write(data)This should do it in Python:
import base64
binary_fc = open(filepath, 'rb').read() # fc aka file_content
base64_utf8_str = base64.b64encode(binary_fc).decode('utf-8')
ext = filepath.split('.')[-1]
dataurl = f'data:image/{ext};base64,{base64_utf8_str}'
Thanks to @cnst comment, we need the prefix data:image/{ext};base64,
Thanks to @ramazanpolat answer, we need the decode('utf-8')
In python3, base64.b64encode returns a bytes instance, so it's necessary to call decode to get a str, if you are working with unicode text.
# Image data from [Wikipedia][1]
>>>image_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x05\x08\x06\x00\x00\x00\x8do&\xe5\x00\x00\x00\x1cIDAT\x08\xd7c\xf8\xff\xff?\xc3\x7f\x06 \x05\xc3 \x12\x84\xd01\xf1\x82X\xcd\x04\x00\x0e\xf55\xcb\xd1\x8e\x0e\x1f\x00\x00\x00\x00IEND\xaeB`\x82'
# String representation of bytes object includes leading "b" and quotes,
# making the uri invalid.
>>> encoded = base64.b64encode(image_data) # Creates a bytes object
>>> 'data:image/png;base64,{}'.format(encoded)
"data:image/png;base64,b'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='"
# Calling .decode() gets us the right representation
>>> encoded = base64.b64encode(image_data).decode('ascii')
>>> 'data:image/png;base64,{}'.format(encoded)
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
If you are working with bytes directly, you can use the output of base64.b64encode without further decoding.
>>> encoded = base64.b64encode(image_data)
>>> b'data:image/png;base64,' + encoded
b'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='