0

I have this following piece of code, to read an image from db and display into html:

import base64

with open("2_questions_html_8f51fe3fa0ef29dd.gif", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

with open('img_test.html', 'w') as f:
    f.write("<!DOCTYPE html><head></head><body>")
    f.write(f'<img src="data:image/png;base64,{encoded_string}"/>')
    f.write('</body></html>')

It doesn't work because the variable encoded_string = b'....[string]....'

If I copy the part ...[string]... inside the b'...[string]...' then it works.

Please give some explanation and how to fix this programmatically (like, how to get the ...[string]... from the encoded_string = b'...string]...'?

Another way for the problem is to write out the base64 data into an image file and paste the src into the img tag. Can you compare the two ways?

Thanks

Dr Linh Chi Nguyen
  • 1,063
  • 1
  • 9
  • 17

0 Answers0