hero video adjustment, image conversion to webp, mobile navigation optimazation
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Convert all service/hero JPGs to WebP at 60-70KB target, update all HTML refs."""
|
||||
import os, glob
|
||||
from PIL import Image
|
||||
|
||||
BASE = os.path.dirname(os.path.dirname(__file__))
|
||||
IMG_DIRS = [
|
||||
os.path.join(BASE, "assets", "images", "services"),
|
||||
os.path.join(BASE, "assets", "images", "hero"),
|
||||
]
|
||||
|
||||
def convert(jpg_path, max_width=900, quality=78):
|
||||
# Hero images get larger max_width
|
||||
if "/hero/" in jpg_path:
|
||||
max_width = 1400
|
||||
quality = 80
|
||||
webp_path = jpg_path.rsplit(".", 1)[0] + ".webp"
|
||||
img = Image.open(jpg_path).convert("RGB")
|
||||
w, h = img.size
|
||||
if w > max_width:
|
||||
img = img.resize((max_width, int(h * max_width / w)), Image.LANCZOS)
|
||||
img.save(webp_path, "WEBP", quality=quality, method=6)
|
||||
kb = os.path.getsize(webp_path) // 1024
|
||||
orig_kb = os.path.getsize(jpg_path) // 1024
|
||||
return webp_path, kb, orig_kb
|
||||
|
||||
converted = []
|
||||
for d in IMG_DIRS:
|
||||
for jpg in glob.glob(os.path.join(d, "*.jpg")):
|
||||
if any(x in jpg for x in ["gen.log", "regen"]):
|
||||
continue
|
||||
webp, kb, orig = convert(jpg)
|
||||
print(f" {os.path.basename(jpg)} {orig}KB -> {kb}KB")
|
||||
converted.append((jpg, webp))
|
||||
|
||||
print(f"\n{len(converted)} images converted")
|
||||
|
||||
# Update all HTML files to reference .webp instead of .jpg (for these image dirs only)
|
||||
html_files = []
|
||||
for root, dirs, files in os.walk(BASE):
|
||||
dirs[:] = [d for d in dirs if d not in [".git", "tools", "assets/videos"]]
|
||||
for f in files:
|
||||
if f.endswith(".html"):
|
||||
html_files.append(os.path.join(root, f))
|
||||
|
||||
updated = 0
|
||||
for html in html_files:
|
||||
with open(html, "r") as f:
|
||||
content = f.read()
|
||||
new = content
|
||||
for jpg, webp in converted:
|
||||
jpg_ref = "/assets/images/" + os.path.relpath(jpg, os.path.join(BASE, "assets", "images"))
|
||||
webp_ref = "/assets/images/" + os.path.relpath(webp, os.path.join(BASE, "assets", "images"))
|
||||
new = new.replace(jpg_ref, webp_ref)
|
||||
if new != content:
|
||||
with open(html, "w") as f:
|
||||
f.write(new)
|
||||
updated += 1
|
||||
|
||||
print(f"{updated} HTML files updated to .webp refs")
|
||||
print("Done.")
|
||||
Reference in New Issue
Block a user