hotschpotsh/parallax-demo/generate_embedded.py

44 lines
1.5 KiB
Python

import base64
import os
def get_base64_src(path, mime_type):
with open(path, "rb") as f:
data = f.read()
return f"data:{mime_type};base64,{base64.b64encode(data).decode('utf-8')}"
try:
# 1. Read Assets
workshop_b64 = get_base64_src("workshop.jpg", "image/jpeg")
depth_b64 = get_base64_src("workshop_depth.png", "image/png")
vase_b64 = get_base64_src("pottery-vase.png", "image/png")
try:
vase_depth_b64 = get_base64_src("pottery-vase_depth.png", "image/png")
except FileNotFoundError:
print("Warning: pottery-vase_depth.png not found. Using placeholder or skipping.")
vase_depth_b64 = depth_b64 # Fallback to something to avoid crash
# 2. Read HTML Template
with open("index.html", "r", encoding="utf-8") as f:
html = f.read()
# Remove the protocol check block for the embedded version
# because embedded base64 images work fine on file:// protocol
import re
html = re.sub(r'// CHECK PROTOCOL[\s\S]*?return;\s+?}', '', html)
# 3. Replace with Base64
html = html.replace("workshop.jpg", workshop_b64)
html = html.replace("workshop_depth.png", depth_b64)
html = html.replace("pottery-vase.png", vase_b64)
html = html.replace("pottery-vase_depth.png", vase_depth_b64)
# 4. Write new file
with open("index_embedded.html", "w", encoding="utf-8") as f:
f.write(html)
print("Successfully created index_embedded.html")
except Exception as e:
print(f"Error: {e}")