How to use Python 3 to convert your images to Base64 encoding
When you encode your images in Base64, your images can be transferred and saved as text.
Although there will be a 37% bloat in space requirements, it can be useful to encode images in Base64.
For example with SAP HANA XS Advanced, you can quickly build an OData service to receive images via JSON.
Given that, let's see how we can encode an image in Base64 encoding with Python 3.
Python 3 codes to encode an image in Base64
After you had decided to encode your image in Base64, you can proceed with coding a utility function with Python 3:
import base64 def get_base64_encoded_image(image_path): with open(image_path, "rb") as img_file: return base64.b64encode(img_file.read()).decode('utf-8')
As shown above, it takes just 4 lines of Python 3 codes to encode an image in Base64.
First, we import the base64 module into our Python script.
Once we have done so, we define a function, get_base64_encoded_image
, that takes an image path as the parameter.
When we have the image path, we use the open function to get a file object to the image that we wish to encode in Base64.
If we can do so successfully, then we use base64.b64encode
on the binary data of the image file.
Before exiting the function, we decode the bytes from base64.b64encode
as a UTF-8 string and return it to the caller.
How to display a Base64 encoded image on the browser
Given that you have saved the Base64 encoded image into your database, how can you display it on the browser?
In order to display a Base64 encoded image on the browser, place the Base64 encoded string in the src attribute of an img tag:
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigkJyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wgARCAAyAGQDAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAwQBAgUA/8QAGgEAAgMBAQAAAAAAAAAAAAAAAgMBBAUABv/aAAwDAQACEAMQAAABYLBhTqkwhL5yh8wiyIlUG1RO1bQxCBE8gmJpCLSLykdjPz7p90aWc9/FYvo0ktcDV5bzoio4GlxAkFheVoOGUxPMcvUzYWZCVxm1ii/j3AWSx/SUFHHsZqLDGdfas02IUURcwXUeWZsjs4N0VmihqCaF0S4iQmwOXZsOAkow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Xm2X7uSCwEcrb7wAoL4ULKQDlV9QSwVmgAqJUV43OmLyMHmUX7WU89blRBaGOL6VFlDmD/U7T+O8AMm+830O/aBHM0WPrV14lBbEvJpO0Q88DIZIevaUA0DdxleGB3wgfpCUboDZ67lnb8DGYyIUyFr6iP1wHkh/MYtTYYa1isOf5jZgoXTKTkLwQjboczkN+IRjF5694s2dynDkIHgXZmrvz1iTBK2UGmbNbhwBu2GkiMomhiKSiqlZO5iP5N04prHHPMuPxThvrCxmTYFravpiZ3BnPF/WYASpWrcxKIeVN/mFkh03k+MwqGYDauktoTrA/wBVnFl/9h1m3XNG83NQ3K9l9cdqmSHV4Q4iguGAVdwFRKpoQ1/eJUSm0YEPmKgRW27YRpaC4a+6+JrIEKZ5/cUMbt4FX334lSNux7avfiZEnJZXtvB4iUiAZAZE9av/AGUf3SFKljQeh7wfUaK3D+QZe0qfjsRRqCnZ0L8TLRGyDs/MsQ7aXT45iXVULKTh9rvpGGO6gOKGs6lZdUWfwRvilJfJQblEtuz/ADBjocAH6itdVhufqWh15q/MUqu4E/NES+irLDSTZFryh18CfccUrV6684lptbobgv014qqERej2lOhDEA0vvMADwoVwPrgtGd0yzebzAigzs4qZXyojtfeK3ALon//Z"/>
When you access the above HTML codes with your browser, you will get the following image:
If you wish to display a different type of image, swap the MIME type.
For example, if you wish to display a png image, you will change:
image/jpeg
to
image/png