how to display images from the ndb datastore using data URI scheme (via passing data to template and not having to make another request)

StackOverflow https://stackoverflow.com/questions/23261462

Question

I store a thumbnail image as a blob property of my entity. I am able to serve the images just fine as described here: Displaying Blob Images in Python (App Engine)

This however requires making another get request and fetching the image by id from the ndb. Since I already have the original entity available at the time I render the img tag with appropriate href attribute, would there exist a way to render the image inline using the Data URI Scheme?

I read the documentation for google images API but there doesn't seem to be a method that would render my image to the required format to include it as a string. I also googled quite extensively but somehow everyone is suggesting the solution for rendering images from the datastore as referenced above. To my mind being able to render the images using the uri scheme would not only save datastore get requests (would most likely be fetched from memcache but still) but most importantly would save visitors of my page a round trip across the ocean which would speed up the serving of the page quite considerably.

Many thanks for all your help!

Was it helpful?

Solution

Use the image service and the images will be served from a high speed image serving service that'll probably be closer to your end users in any case then anything you could arrange yourself in GAE.

https://developers.google.com/appengine/docs/python/images/functions

get_serving_url(blob_key, size=None, crop=False, secure_url=None)

Returns a URL that serves the image.

This URL format allows dynamic resizing and cropping, so you don't need to store different image sizes on the server. Images are served with low latency from a highly optimized, cookieless infrastructure.

This also avoids another hit to the database, as you'd store the URL and serve that to the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top