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.
cancel
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:

CREATE COLUMN TABLE "CAMERA_FEED_DB"."CameraFeed.data::feed.Image"(
"id" NVARCHAR(20) NOT NULL,
"camera_id" NVARCHAR(20),
"payload" NCLOB MEMORY THRESHOLD 1000,
"createdat" LONGDATE CS_LONGDATE,
PRIMARY KEY (
"id"
)
)


Service definition: CameraFeed/service/dataservice.xsodata


service {
"CameraFeed.data::feed.Image" as "Image";
}


Given these configurations, let's assume that we have an ODATA endpoint at https://sampleusecaseserver.jp1.hana.ondemand.com/CameraFeed/service/dataservice.xsodata/Image. 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:

USERNAME='my_project_user'
PASSWORD='ABcD1234'
CAMERA_FEED_URL='https://sampleusecaseserver.jp1.hana.ondemand.com/CameraFeed/service/dataservice.xsodata/Image'
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(img_file.read()).decode('utf-8')

image_base64 = get_base64_encoded_image(image_path)

now = current_time_millis()

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

response = requests.post(url=CAMERA_FEED_URL, 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 CameraFeed.data::feed.Image table.