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')
How to base64 encode an image using python - Stack Overflow
How to encode a image in Python to Base64? - Stack Overflow
Python 3 - Base64 Encoding - Stack Overflow
I noticed ChatGPT can decode Base64. So I asked it how it's able to do that in Base64 (otherwise it denies in has this ability). This is the response. Is it bullshit?
Try this code
Image base64 encoded format
Python code:
import os
import base64
image = 'test.jpg'
encoded_string = ""
with open(image, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = encoded_string
This code does the job:
from PIL import Image
import io
import base64
import cStringIO
image = Image.open(io.BytesIO(imagebuffer))
encodingbuffer = cStringIO.StringIO()
image.save(encodingbuffer, format="JPEG")
encodedimage = base64.b64encode(encodingbuffer.getvalue())
You can encode the RGB directly to jpg in memory and create a base64 encoding of this.
jpg_img = cv2.imencode('.jpg', img)
b64_string = base64.b64encode(jpg_img[1]).decode('utf-8')
Full example:
import cv2
import base64
img = cv2.imread('test_image.jpg')
jpg_img = cv2.imencode('.jpg', img)
b64_string = base64.b64encode(jpg_img[1]).decode('utf-8')
The base 64 string should be decodable with https://codebeautify.org/base64-to-image-converter
Try This Method :- RGB image base64 encode/decode
import cStringIO
import PIL.Image
def encode_img(img_fn):
with open(img_fn, "rb") as f:
data = f.read()
return data.encode("base64")
def decode_img(img_base64):
decode_str = img_base64.decode("base64")
file_like = cStringIO.StringIO(decode_str)
img = PIL.Image.open(file_like)
# rgb_img[c, r] is the pixel values.
rgb_img = img.convert("RGB")
return rgb_img
I followed the solution from this link it seems to work for me. So when you read the image in binary convert it to a string and then just encode the string with base64. The following solution is from the link above. Here is the tested code.
import base64
image = open(image, 'rb')
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
Finally I found a code running on Python 3.7:
# Get the image
image = open(path, 'rb')
image_read = image_read()
# Get the Byte-Version of the image
image_64_encode = base64.b64encode(image_read)
# Convert it to a readable utf-8 code (a String)
image_encoded = image_64_encode.decode('utf-8')
return image_encoded