How to Convert Image to Base64 String in Python

How to Convert Image to Base64 String in Python

Base64 encoding enables us to convert bytes containing binary or text data to ASCII characters. By encoding the data, we improve the chances of it being processed correctly by various systems. Some systems don’t understand the data in bytes format, so we have to convert the data.

The purpose of the base64.b64encode() method in Python is to convert the binary data into ASCII-safe “text”. But Python strongly disagrees with that. Python is now very strict about this that bytes.encode() method doesn’t even exist, and so ‘xyz’.encode(‘base64’) would raise an AttributeError.

Now, let’s understand the base64 encoding.

What is Base64 Encoding?

Base64 encoding is the type of conversion of bytes into ASCII characters. In maths, the base of a number system indicates how many different characters represent numbers.

The name of base64 encoding comes directly from a mathematical definition of the bases: we have 64 characters that represent the numbers.

The Base64 character set contains the following.

  1. 26 uppercase letters
  2. 26 lowercase letters
  3. 10 numbers
  4. + and / for new lines

When a computer converts Base64 characters to binary, each Base64 character represents the 6 bits of information.

Convert Image to Base64 String in Python

To convert the Image to Base64 String in Python, we have to use the Python base64 module that provides b64encode() method. Python base64.b64encode() function encodes a string using Base64 and returns that string.


base64.b64encode(s[, altchars])


The function takes s as an input, and it is the required parameter.

The Optional altchars must be a string of at least length 2 (additional characters are ignored), which defines the alternative alphabet for the + and / characters. This enables an application to, for example, generate URL or filesystem safe Base64 strings. The default is None, for which a standard Base64 alphabet is used.

Return Value

The b64encode() function returns the encoded string.

Example of converting Image to base64 in Python

import base64

with open("grayimage.png", "rb") as img_file:
    b64_string = base64.b64encode(

In my project folder, I have an image called grayimage.png. Please make sure that your image path is correct.

In this example, at first, we opened our file in ‘rb’ mode. Then we read the image file.

Python with open() function opens a file and then we can perform whatever operations we want. It automatically closes the file when we won’t need it anymore.

Then encoded it with b64encode() method. You must read an image file before you encode it.

I am getting the long string in the output in the terminal, like the following.


Your string has been printed. But in the starting position of your base64 string, there is a b’.

Or you can say your base64 encoded string is in the pair of single quotation.

To remove b’ from the prefix of base64 code in Python, you can use string.encode(‘utf-8’) method.

import base64

with open("grayimage.png", "rb") as img_file:
    b64_string = base64.b64encode(

That is it for how to convert Image to base64 string in Python example.

See also

Convert PIL Image to Grayscale

Convert PIL Image to Numpy Array

How to crop an image in Python

How to resize an image in Python

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.