Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

Convert Image to Base64 Encoding - Python

Images are the important part of web applications.
Which are heavily used in some major application and ,some of the best applications are made on the base of the images.
Lets learn how to change the image from an URL to be converted to base64 encoded string which can be used directly to the img tag under html.
Lets see the code in python to do the same.
import base64
import requests
url = "http://images.freeimages.com/images/previews/118/bicycle-sign-3-1442216.jpg"
response = requests.get(url)
convertedData = ("data:" + response.headers['Content-Type'] + ";" + "base64," +str(base64.b64encode(response.content).decode("utf-8")))
Now you have the string inside variable called converted data, which holds base64 encoded string for the image mentioned in the url variable.
print convertedData

Top #3 Articles

Most Read