Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # date: 2025.10.02
- # [Staging Ground: Display images from zip file in HTML table without extracting them - Stack Overflow](https://stackoverflow.com/staging-ground/79781002#comment140773792_79781002)
- # - example image from Wikipedia [Lenna](https://en.wikipedia.org/wiki/Lenna) -
- # wget https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png -O lenna.png
- # - create zip file -
- # zip data.zip lenna.png
- # - run www server with index.html -
- # python3 -m http.server
- # tested on Linux Mint, Python 3.13
- import zipfile
- import base64
- filename_zip = "data.zip"
- filename_image = "lenna.png"
- filename_html = "index.html"
- with zipfile.ZipFile(filename_zip) as zip_file:
- with zip_file.open(filename_image) as image_file:
- image_bytes = image_file.read()
- base64_bytes = base64.b64encode(image_bytes)
- base64_string = base64_bytes.decode("utf-8")
- html = f'<img src="data:image/png;base64, {base64_string}">'
- with open(filename_html, "w") as html_file:
- html_file.write(html)
Add Comment
Please, Sign In to add comment