2

I have saved in a database a string return by the javascript method toDataURL. An exemple is available here: http://pastebin.com/0Qu8rngD

I need to return the image in a django response. Something like:

return HttpResponse(image, mimetype='image/png')

I have tried many methods with base64decode, urlsafe_b64decode, Image ... with no success. The navigator does not display the image, it can't read the response data.

Of course I can display my image in a HTML page with <img src="{{image}}">, it works well.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895

1 Answers1

5

The image is base64 encoded in the data uri; decode the image first:

import base64

...    

data_uri = 'data:...'
image_data = data_uri.partition('base64,')[2]
binary = base64.b64decode(image_data)
return HttpResponse(binary, mimetype='image/png')