Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
Previously, I showed how to use Python 3 to get data from a SAP HANA OData endpoint. Since we can quickly build OData endpoints to back the data needed for our application on SAP HANA, it can be useful to be able to save an image via a JSON payload.

For example, we can build an image similarity scoring page on our online shop. Once we run an image through an inference model, we can then save that image along with the classification labels in our HANA database for future retrievals.

Since there are many machine learning libraries in Python, you may want to be able to use Python 3 to save an image via a JSON payload to a SAP HANA OData endpoint.

Given these points, this post shows how we can use Python 3 to save an image via a JSON payload to a SAP HANA OData endpoint.

A sample photo taking use case

In order to keep things simple, suppose that we are building a camera feed that captures an image of a scene periodically. After capturing an image, the Python 3 application will save it to our SAP HANA instance.

Given that, let's expose a database table as an OData endpoint through SAP HANA XS Advanced.

Table data definition:

"camera_id" NVARCHAR(20),

Service definition: CameraFeed/service/dataservice.xsodata

service {
"" as "Image";

Given these configurations, let's assume that we have an ODATA endpoint at In addition to that, we can also use a user, with my_project_user and ABcD1234, to perform CRUD on the table.

Writing the Python 3 codes to save an image via a JSON payload to a SAP HANA OData endpoint

Now that we have the OData endpoint ready, we can look at how we can write the Python 3 codes to save our image to a SAP HANA OData endpoint.

In order to achieve this objective, we need the code to do the following:

Given these points, let's see how we can use Python 3 to send an image at /captured-image/001.jpg to our OData endpoint:

CAMERA_ID = 'cam1'
image_path = '/captured-image/001.jpg'

request_headers = {
'Content-Type': 'application/json'

current_time_millis = lambda: int(round(time.time() * 1000))

def get_base64_encoded_image(image_path):
with open(image_path, "rb") as img_file:
return base64.b64encode('utf-8')

image_base64 = get_base64_encoded_image(image_path)

now = current_time_millis()

image_payload_body={"id": "%s" % now,
"createdat":"/Date(%s)/" % now}

response =, json=image_payload_body, headers=request_headers, auth=(USERNAME, PASSWORD))

When you run the above Python 3 script, you should be able to save /captured-image/001.jpg as a Base64 encoded image into the table.