I've a url to gravatar image as
url = "http://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
And I want to generate data:URI for it like following in python
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==
import base64
import requests
url = "https://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
response = requests.get(url)
content_type = response.headers["content-type"]
encoded_body = base64.b64encode(response.content)
print("data:{};base64,{}".format(content_type, encoded_body.decode()))
Response:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==