From 0f232f57d86712ba1ea697747d0e345bab739a42 Mon Sep 17 00:00:00 2001 From: blamster19 Date: Mon, 29 Sep 2025 19:48:29 +0200 Subject: [PATCH] add support for base64 embedded images The HTML tag supports images embedded directly into HTML body using base64 encoding. This commit adds support for MIME types Image/jpeg, Image/png and Image/gif. --- tkhtmlview/html_parser.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tkhtmlview/html_parser.py b/tkhtmlview/html_parser.py index 79e2d68..c053e15 100644 --- a/tkhtmlview/html_parser.py +++ b/tkhtmlview/html_parser.py @@ -11,6 +11,7 @@ from collections import OrderedDict import requests from io import BytesIO +import base64 # __________________________________________________________________________________________________ @@ -543,6 +544,24 @@ def handle_starttag(self, tag, attrs): except: pass + if attrs[HTML.Attrs.SRC].startswith(("data:image/jpeg;base64,")): + try: + image = Image.open( + BytesIO(base64.b64decode(attrs[HTML.Attrs.SRC][23:].encode("utf-8"))) + ) + self.cached_images[attrs[HTML.Attrs.SRC]] = deepcopy(image) + except: + pass + + if attrs[HTML.Attrs.SRC].startswith(("data:image/png;base64,", "data:image/gif;base64,")): + try: + image = Image.open( + BytesIO(base64.b64decode(attrs[HTML.Attrs.SRC][22:].encode("utf-8"))) + ) + self.cached_images[attrs[HTML.Attrs.SRC]] = deepcopy(image) + except: + pass + if attrs[HTML.Attrs.SRC] in self.cached_images.keys(): image = deepcopy(self.cached_images[attrs[HTML.Attrs.SRC]]) elif os.path.exists(attrs[HTML.Attrs.SRC]):