Convert Image to String and vice-versa in Python

When we speak of "converting an image into string format and back again in Python", this refers to converting image files to something that can be stored and managed using strings of characters, before eventually returning it back into its original image format.

Converting Image to String − Converting images to strings involves transcribing their binary data into text format for transmittal or storage over network protocols that require text-based images. This conversion method can help us transmit or store an image more efficiently when working with APIs, databases or sending images over network protocols requiring text data transmission or storage.

The resultant string is typically composed of representations of an image's binary data using specific encoding schemes such as base64 encoding. This method ensures that each piece of binary information is represented using ASCII characters for easy transmission or storage as strings.

Converting String to Image  Converting strings back to images requires decoding encoded strings back into their binary data representation, enabling us to reconstruct image files using their encoded representation.

For this exercise, we will convert Tutorialspoint's logo in JPEG format into string form and back again using that string form to restore back into an image format. We will use the following iamge as input file:

Method 1: Base64 Encoding/Decoding

Base64 encoding is an increasingly popular method for representing binary data such as images in string format. Python offers its base64 module to encode and decode using this algorithm.

To Convert an Image to a String

The below code reads an image file named "tpt_logo.jpg" in binary mode and encodes its content using base64.b64encode() function into a base64 string using write() method before saving to a text file named encoded_image.txt using write() method - followed by printing of message confirming its storage in text file as base64 string representation for future use or transmission. Essentially this code converts image into base64-encoded string representation before saving to text file for future use or transmission or transmission.


import base64

# Read image file
with open('tpt_logo.jpg', 'rb') as image_file:
    encoded_string = base64.b64encode('utf-8')

# Save the encoded string in a text file
with open('encoded_image.txt', 'w') as file:

print("Encoded string saved in 'encoded_image.txt'.")


Encoded string saved in 'encoded_image.txt'.

To Convert a String Back to an Image

The below code reads an encoded string from "encoded_image.txt", decodes it using base64.b64decode() to obtain its binary data, then an image object is created from those bytes using PIL library's function with BytesIO providing file-like access. Finally, an image object is either displayed or shown so you can view it on screen; effectively decoding, reconstructing and visualizing an encoded image string before finally showing it for visualization on screen


import base64
from io import BytesIO
from PIL import Image

# Read the encoded string from the text file
with open('encoded_image.txt', 'r') as file:
    encoded_string =

# Decode the string
decoded_string = base64.b64decode(encoded_string)

# Create an image object from the decoded bytes
image =

# Display or save the image


Method 2: Using Zlib and Binascii

Zlib is a Python compression library that offers functions for data compression and decompression using the DEFLATE algorithm, with efficient capabilities for files or network transmissions. Furthermore, its capabilities of compressing/decompressing streams (strings/binary data) makes this invaluable when dealing with large amounts of information.

Python's binascii module provides functionality to convert binary data to text-based protocols or formats that make data human readable and transportable; making data human readable or transportable using textual protocols or formats such as Hexadecimal, Base64 or Quoted Printable representations easily. Binascii is often employed when working with large datasets that require storage or transmission using text-based protocols or formats when dealing with binary data which requires conversion to textual formats during transmission or storage.

To Convert an Image to a String

In the below code, we read an image file as mentioned above (Tutorials point logo) and compress its data with the help of the zlib library. After this the compressed data is converted into a hexadecimal string with the help of binascii library and then stored in text file named "compressed_image.txt".


import zlib
import binascii

# Read image file
with open('tpt_logo.jpg', 'rb') as image_file:
    image_data =

# Compress the image data using zlib.
compressed_data = zlib.compress(image_data)

# Convert the compressed data to a hexadecimal string.
hex_string = binascii.hexlify(compressed_data).decode('utf-8')

# Save the hexadecimal string in a text file.
with open('compressed_image.txt', 'w') as file:

print("Hexadecimal string saved in 'compressed_image.txt'.")


Hexadecimal string saved in 'compressed_image.txt'.

To Convert a String Back to an Image

The below code reads the hexadecimal string from the "compressed_image.txt" file. The string is converted back to compressed data using binascii.unhexlify(). Then, zlib.decompress() is used to decompress the data. The decompressed data is written to a new file named "reconstructed_image.jpg" in binary mode ('wb'). The output indicates that the reconstructed image has been saved successfully.

Note − Here, the image reconstruction code saves its results in the same folder and does not directly display them to user; rather we manually opened it and displayed below as output.


import zlib
import binascii

# Read the hexadecimal string from the text file.
with open('compressed_image.txt', 'r') as file:
    hex_string =

# Convert the hexadecimal string to compressed data.
compressed_data = binascii.unhexlify(hex_string)

# Decompress the data using zlib
decompressed_data = zlib.decompress(compressed_data)

# Create a new file and write the decompressed data to it.
with open('reconstructed_image.jpg', 'wb') as image_file:

print("Reconstructed image saved as 'reconstructed_image.jpg'.")


Reconstructed image saved as 'reconstructed_image.jpg'.


There are different ways in python to convert images to strings and mainly include base64 encoding/decoding as well as compression with zlib and binascii to convert binary representation to text representation conversion. We have discussed both these approaches in this tutorial. Irrespective of the approach, at the end of the day both provide us with options for storing, transmitting and processing image data more efficiently.

Updated on: 18-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started