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
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ converting-image-to-base64-in-python
Converting Image To Base64 In Python
March 16, 2023 - In the above example, we have saved the encoded image to a text file called "image.txt" using the "write" function. We have used the "decode" function to convert the byte string to a string. In this article, we have learned how to convert an image to base64 format in Python using the Pillow and base64 modules.
Discussions

python - How to convert PIL Image.image object to base64 string? - Stack Overflow
I am trying to manipulate a base64 encoded image in such a way as to rotate it at 90 angle. After this manipulation, I want to convert it back to base64 string. But unfortunately unable to achieve ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert byte[] to base64 and ASCII in Python - Stack Overflow
How do I to convert and array of byte to base64 string and/or ASCII. I can do this easily in C#, but can't seem to do this in Python More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to encode a image in Python to Base64? - Stack Overflow
The base 64 string should be decodable with https://codebeautify.org/base64-to-image-converter More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert Python numpy array to base64 output - Stack Overflow
I basically need to do this but in Python instead of Javascript. I receive a base64 encoded string from a socketio connection, convert it to uint8 and work on it, then need to convert it to base64 More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

Can I convert Base64 back to an image?
Yes, Base64 encoding is fully reversible. You can convert a Base64 string back to the original image by decoding it. In a browser, you can simply paste the data URI into the address bar to view the image. In code, use atob() in JavaScript or base64.b64decode() in Python to decode the string back to binary image data.
๐ŸŒ
pixelpanda.ai
pixelpanda.ai โ€บ home โ€บ free tools โ€บ image to base64
Image to Base64 โ€” Convert Any Image to Base64 Online Free | ...
Why is the Base64 string larger than the original file?
Base64 encoding increases data size by approximately 33% because it converts every 3 bytes of binary data into 4 ASCII characters. This is a fundamental property of the encoding โ€” it's the cost of representing binary data as text. For small images (under 10KB), this overhead is offset by eliminating an HTTP request. For larger images, external files are more efficient.
๐ŸŒ
pixelpanda.ai
pixelpanda.ai โ€บ home โ€บ free tools โ€บ image to base64
Image to Base64 โ€” Convert Any Image to Base64 Online Free | ...
Is Base64 encoding secure?
Base64 is an encoding scheme, not encryption โ€” it provides zero security. Anyone can decode a Base64 string back to the original data. It's designed for data transport compatibility, not confidentiality. Never use Base64 as a way to "hide" or protect sensitive image content.
๐ŸŒ
pixelpanda.ai
pixelpanda.ai โ€บ home โ€บ free tools โ€บ image to base64
Image to Base64 โ€” Convert Any Image to Base64 Online Free | ...
๐ŸŒ
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 - You can also use the Pillow module to convert an image to a base64-encoded string. First, install the module by running the following command in your terminal. ... Now import and use the module as follows.
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ python โ€บ examples โ€บ faq โ€บ python-how-to-convert-image-to-base64-string-and-viceversa
How to Convert Images to Base64 Strings (and Back) in Python | Tutorial Reference
Converting images to Base64 strings in Python is straightforward using the base64 module for encoding (base64.b64encode) and decoding (base64.decodebytes or base64.b64decode). Remember to handle file I/O in binary mode ('rb' or 'wb') and decode/encode between bytes and str as needed using .decode() ...
๐ŸŒ
PixelPanda
pixelpanda.ai โ€บ home โ€บ free tools โ€บ image to base64
Image to Base64 โ€” Convert Any Image to Base64 Online Free | PixelPanda
Python's standard library makes base64 encoding straightforward. Read the file as binary, encode it, and decode the bytes to a UTF-8 string.
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 - base64.b64encode(): Encodes the binary data into a Base64 string. .decode('utf-8'): Converts the result into a readable string format. Without .decode('utf-8'), the result would be in a byte format.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - We finally get the string representation of the Base64 conversion by decoding the base64_bytes as ASCII. Note: Be sure to use the same encoding format when converting from string to bytes, and from bytes to string.
๐ŸŒ
Base64.Guru
base64.guru โ€บ converter โ€บ decode โ€บ image
Base64 to Image | Base64 Decode | Base64 Converter | Base64
Hello Dan! Thank you for your kind words. The Base64 string doesn't contain such information. So, first of all you have to decode it to original data and try to guess the file type. To do this, you will need to use file signatures (also known as "Magic Numbers" or "Magic Bytes").
๐ŸŒ
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().
๐ŸŒ
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 ...
๐ŸŒ
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 conversion and resizing.
๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ base64 converter โ€บ base64 encode
Image to Base64 | Base64 Encode | Base64 Converter | Base64
Convert image to Base64 online and use the result string as data URI, img src, CSS background-url, and others. Sometimes you have to send or output an image within a text document (for example, HTML, CSS, JSON, XML), but you cannot do this because binary characters will damage the syntax of the text document.
๐ŸŒ
Formspree
formspree.io โ€บ blog โ€บ image-to-base64
How to Convert an Image URL to Base64 | Formspree
August 28, 2024 - To convert image URLs to base64 in Python, you will use the requests library to fetch the image data from a URL. Next, you will read the image data into a bytes object. Then, you will use the base64 library to encode the bytes object into a ...
๐ŸŒ
Browserling
browserling.com โ€บ tools โ€บ image-to-base64
Image to Base64 Converter - Encode Images to Base64 - Online - Browserling Web Developer Tools
You can also use this converter to create test cases for various image formats. If your application depends on processing and converting images from one format to another, then you can create a set of tests that compare the binary image with the encoded base64 version of it and the other way around.
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-convert-python-bytes-to-image
5 Best Ways to Convert Python Bytes to Image โ€“ Be on the Right Side of Change
February 23, 2024 - This one-liner converts the byte data into a Base64 string and forms an <img> HTML tag that can be used in any webpage to display the image.