Python - Convert Image to String and vice-versa Last Updated : 30 Dec, 2024 Summarize Suggest changes Share Like Article Like Report To store or transfer an Image to some we need to convert it into a string such that the string should portray the image which we give as input. In Python we have a lot of functions in Python available to convert an image into a string.Image used:Image UsedConvert Image To StringTo convert an image to a string in Python, we can turn the image into a special text format called base64. This allows us to store or send the image as text which is easier to handle in some situations. We can easily convert it back to an image later. Python import base64 # Encode the image to a base64 string. with open("food.webp", "rb") as img: s = base64.b64encode(img.read()) print(s) # Save the encoded string to a file. with open('encode.bin', "wb") as f: f.write(s) Output:This Is The Output Of Image That is Converted To String Using Base64Explanation:We import the base64 module to use its b64encode() method for encoding data.Image is opened in binary read mode (rb) to read its raw content.Image is read using image2string.read() and then encoded into a base64 string with base64.b64encode().Finally, the base64-encoded string is printed to view the image in text form.Note: Here we got the output, but if we notice at the start of the string, we get a b'. This indicates a base64 encoded string in a pair of single quotations. To remove that, we can replace the print statement with print(my_string.decode('utf-8')).Convert String To ImageTo convert a base64-encoded string back into an image, we decode the string to retrieve the original binary data. Then, we write the binary data into a new image file. This process allows us to restore the image from its base64 representation.Example: Python import base64 # For decoding. f = open('encode.bin', 'rb') # Open encoded file. byte = f.read() # Read data. f.close() decode = open('stringtoimage.webp', 'wb') # Open image file to save. decode.write(base64.b64decode(byte)) # Decode and write data. decode.close() Output:OutputExplanation:First, we import the base64 module.Open the binary file containing the base64 string in read-binary mode (rb) read the data into a variable and close the file.Open a new image file (e.g., "myimage.png") in write-binary mode (wb) and decode the base64 string using base64.b64decode().Write the decoded binary data into the file and close it with .close(). Advertise with us Next Article Convert Object to String in Python N nishanthec19 Follow Similar Reads Convert the .GIF to .BMP and it's vice-versa in Python Sometimes it is required to attach the Image where we required an image file with the specified extension. And we have the image with the different extension which needs to be converted with a specified extension like in this we will convert the image having Extension of .bmp to .gif and Vice-Versa. 3 min read Convert files from jpg to png and vice versa using Python Prerequisite: Pillow Library Sometime it is required to attach the Image where we required an image file with the specified extension. And we have the image with a different extension which needs to be converted with a specified extension like in this we will convert the image having an Extension o 3 min read Convert image to binary using Python In this article, we are going to convert the image into its binary form. A binary image is a monochromatic image that consists of pixels that can have one of exactly two colors, usually black and white. Binary images are also called bi-level or two-level. This means that each pixel is stored as a si 1 min read Convert Object to String in Python Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() m 2 min read Convert PDF to Image using Python Many tools are available on the internet for converting a PDF to an image. In this article, we are going to write code for converting pdf to image and make a handy application in python. Before writing the code we need to install the required module pdf2image and poppler.Modules Neededpdf2image 1.14 2 min read Convert Blob Image to PNG and JPG Using Python We are given a task to convert blob images to png and jpg with Python. In this article, we will see how we can convert blob images to PNG and JPG with Python. Convert Blob Image to PNG and JPG With PythonBelow are step-by-step procedures by which we can convert blob images to PNG and JPG with Python 3 min read Article Tags : Python python-string Practice Tags : python Like