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 Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-image-to-string-and-vice-versa
Python - Convert Image to String and vice-versa - GeeksforGeeks
July 23, 2025 - Image is read using image2string.read() and then encoded into a base64 string with base64.b64encode().
Discussions

How to base64 encode an image using python - Stack Overflow
I have a stream of data that comes from a device and I need to convert it into a jpeg and then base64 encode it to transfer it over the network. My Python 2.7 code so far looks like this: from PIL More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to encode a image in Python to Base64? - Stack Overflow
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') ... import cv2 import base64 img = cv2.imread('test_image.jpg') jpg_img = cv2.imencode('.jpg', img) b64_string ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python 3 - Base64 Encoding - Stack Overflow
I have some images, that I need to give to a server using JSON. I decided to use Base64 as encoding system. In Python 2, I could simply use: with open(path, "rb") as imageFile: img_file = ima... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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?
Hey u/jsideris , if your post is a ChatGPT conversation screenshot, please reply with the conversation link or prompt. Thanks! We have a public discord server . There's a free Chatgpt bot, Open Assistant bot (Open-source model), AI image generator bot, Perplexity AI bot, ๐Ÿค– GPT-4 bot ( Now with Visual capabilities (cloud vision)! ) and channel for latest prompts! New Addition: Adobe Firefly bot and Eleven Labs cloning bot! So why not join us? NEW: Spend 20 minutes building an AI presentation | $1,000 weekly prize pool PSA: For any Chatgpt-related issues email support@openai.com I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/ChatGPT
89
68
August 29, 2023
๐ŸŒ
Medium
medium.com โ€บ @maggi.giuseppe โ€บ python-embedding-images-in-json-objects-with-base64-8579059d80d7
Python: embedding images in JSON objects with base64 | by Giuseppe Maggi | Medium
March 12, 2024 - As shown, we shortened the encoded string in the content property to make the representation more concise. The following code shows how to extract the image we included in the JSON file. Obviously, the code depends on the structure we created for the object. In fact, we get the value of content of the first element in the images list: from io import BytesIO import base64, json from PIL import Image with open('embedded.json', 'r') as f: data_received=json.load(f) im = Image.open(BytesIO(base64.b64decode(data_received['images'][0]['content']))) im.save('picture_out.jpg', 'JPEG')
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ encoding-image-with-base64
Encoding an Image File With BASE64 in Python - AskPython
April 29, 2023 - To encode an image, first, import the base64 module. Then, open the image file in binary mode and read its content. Use the base64.b64encode() function to convert the image content into a Base64 string.
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals โ€บ oop
Base64 Encoding and Decoding Using Python | Envato Tuts+
April 11, 2022 - (I'm assuming that the name of the image is deer.gif.) The first thing we have to do in order to use Base64 in Python is to import the base64 module: ... In order to encode the image, we simply use the function base64.b64encode(s).
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ converting-image-to-base64-in-python
Converting Image To Base64 In Python
March 16, 2023 - To convert an image to base64 format in Python, you can follow these simple steps: Step 1. Install the Pillow module ยท If you do not have the Pillow module installed, you can install it using pip. Open a command prompt and type the following command: ... Step 2. Load the Image ยท Once you have installed the Pillow module, you can load the image that you want to encode in base64 format using the Image module.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/pythonprojects2 โ€บ how to convert image to base64 in python?
r/PythonProjects2 on Reddit: How to convert image to base64 in Python?
October 27, 2024 - import base64 # Step 1: Import the base64 library # Step 2: Open the image in binary mode with open("your_image.jpg", "rb") as image_file: image_data = image_file.read() # Step 3: Encode the binary data into Base64 format base64_string = base64.b6...
๐ŸŒ
Cloudinary
cloudinary.com โ€บ home โ€บ image conversion to base64 in python: a comprehensive guide
Image Conversion to Base64 in Python: A Comprehensive Guide | Cloudinary
April 24, 2026 - Alternatively, you can convert images to Base64 directly in Python with the Base64 package included in the Python Standard Library. Hereโ€™s an example: import base64 def convert_image_to_base64(image_path): """This takes a file path and returns ...
๐ŸŒ
Code Beautify
codebeautify.org โ€บ blog โ€บ how-to-convert-base64-to-image-using-python
How to Convert Base64 to Image Using Python
February 22, 2024 - Remember to install the necessary packages before running the code: pip install pillow ยท To convert a Base64 string to an image in Python, we can use the base64 module to decode the string and the PIL (Pillow) library to work with images.
๐ŸŒ
Medium
annacsmedeiros.medium.com โ€บ efficient-image-processing-in-python-a-straightforward-guide-to-base64-and-numpy-conversions-e9e3aac13312
Efficient Image Processing in Python: A Straightforward Guide to Base64 and Numpy Conversions | by Anna C S Medeiros | Medium
March 30, 2024 - This method can be universally applied to handle images of various file extensions, maintaining the integrity and fidelity of the original data across different image formats. import cv2 import base64 encode_params = [int(cv2.IMWRITE_PNG_COMPRESSION), 9] _, buffer = cv2.imencode('.png', my_opencv_image, encode_params) b64_bytearr = base64.b64encode(buffer).decode("utf-8") b64 = str(b64_bytearr, 'utf-8')
๐ŸŒ
Tech Coil
techcoil.com โ€บ blog โ€บ how-to-use-python-3-to-convert-your-images-to-base64-encoding
How to use Python 3 to convert your images to Base64 encoding
May 11, 2020 - First, we import the base64 module into our Python script. Once we have done so, we define a function, get_base64_encoded_image, that takes an image path as the parameter.
๐ŸŒ
CodeWords
codewords.ai โ€บ blog โ€บ image-convert-base64
Image convert base64: encode and decode in Python | CodeWords
June 3, 2026 - Base64 encoding converts binary image data to ASCII text, increasing size by ~33% but enabling transport through text-only channels (JSON, XML, email). Python's base64 module handles encoding/decoding in two lines; pair it with Pillow for format ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ convert-image-to-base64-string-in-python
Convert an Image to Base64 String and vice versa in Python | bobbyhadz
April 11, 2024 - We first open the image file in rb (read bytes) mode. The next step is to use the base64.b64encode() method to encode the bytes-like image object using Bas64, returning a bytes object.
๐ŸŒ
Crashlaker
crashlaker.github.io โ€บ python โ€บ 2020 โ€บ 08 โ€บ 26 โ€บ image_encode_decode_base64.html
Image encode/decode base64 - Carlos Aguni Personal Blog
August 26, 2020 - import base64 import io from PIL import Image def img_to_txt(filename): msg = b"<plain_txt_msg:img>" with open(filename, "rb") as imageFile: msg = msg + base64.b64encode(imageFile.read()) msg = msg + b"<!plain_txt_msg>" return msg def decode_img(msg): msg = msg[msg.find(b"<plain_txt_msg:img>")+len(b"<plain_txt_msg:img>"): msg.find(b"<!plain_txt_msg>")] msg = base64.b64decode(msg) buf = io.BytesIO(msg) img = Image.open(buf) return img filename = 'test.png' msg = img_to_txt(filename) img = decode_img(msg) img.show()
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Base64 encoding is a popular technique ... cannot process raw binary data but would readily handle text. With Python, we can use the base64 module to Base64 encode and decode text and binary data....
๐ŸŒ
sverrirs.blog
blog.sverrirs.com โ€บ 2013 โ€บ 05 โ€บ base64-encoding-image-files-using-python.html
Base64 encoding image files using Python - sverrirs.blog
June 16, 2015 - # Remove any query string elements, basically everything following # a question (?) mark qs_split = image_path.split("?") image_path = qs_split[0] file_name = os.path.join( base_path, image_path) file_name = urllib.unquote(file_name) print "Encoding image: "+file_name encoded_string = "" with open(file_name, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) return encoded_string
๐ŸŒ
Aspose
products.aspose.com โ€บ svg โ€บ python-net โ€บ base64-encoder
Encode Image to Base64 โ€“ Python Code and online converter
January 1, 2021 - This article considers how to encode an image file to a Base64 string using Aspose.SVG for Python via .NET API.