1

I am trying to convert a ".tiff" image to base64 encoded url. However its not wroking in html when I included it in image src.

I have used the following code to get my base64 string

content_type_mapper = {
    ".jpg": "image/jpg",
    ".jpeg": "image/jpeg",
    ".tiff": "image/tiff",
    ".pdf": "application/pdf",
    ".png": "image/png",
    ".tif": "image/tiff"
}
def get_base64(path, content_type):
    with open(path, "rb") as img_file:
        my_string = base64.b64encode(img_file.read())
    my_string = f"data:{content_type_mapper[content_type]};base64," + my_string.decode('ascii')
    return my_string

the resulting string from here cant'e be rendered properly in brwoser. I have tried change the content type to image/png, image/application/octet-stream, but didnt work Anyone worked on this before?

Yash
  • 223
  • 2
  • 11
  • Your code works for `.jpg` for me – Finomnis Jul 11 '22 at 14:34
  • I don't think Firefox or Chrome either have a `tiff` renderer built-in. For me, at least, it immediately attempts to download the item instead. – Finomnis Jul 11 '22 at 14:37
  • Does this answer your question? [How would I display a TIFF images in all web browsers?](https://stackoverflow.com/questions/2176991/how-would-i-display-a-tiff-images-in-all-web-browsers) – Finomnis Jul 11 '22 at 14:38

0 Answers0