Skip to content

Commit

Permalink
tweak image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ugomeda committed Jan 27, 2025
1 parent 71d875c commit e152fe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 10 additions & 5 deletions inkplate_dashboard/chrome.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import io
import itertools
import os
Expand All @@ -6,8 +7,6 @@

from PIL import Image

from inkplate_dashboard.config import DisplayConfiguration

GOOGLE_CHROME_PATHS = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/usr/bin/chromium",
Expand All @@ -22,7 +21,10 @@ def get_chrome_path() -> str:
raise Exception("Could not find Chrome/Chromium path")


def screenshot_display(display: DisplayConfiguration) -> bytes:
def screenshot_display() -> tuple[bytes, str]:
"""Makes a screenshot of the html view and return the PNG
image and a hash.
"""
with tempfile.TemporaryDirectory() as tmp_dir:
screenshot_path = os.path.join(tmp_dir, "screenshot.png")
subprocess.run(
Expand All @@ -37,10 +39,11 @@ def screenshot_display(display: DisplayConfiguration) -> bytes:
f"--screenshot={screenshot_path}",
"--window-size=825,1200",
"--virtual-time-budget=10000",
"--timeout=10000", # load fonts
"--timeout=5000",
"http://127.0.0.1:8000/live/html",
],
check=False,
timeout=10,
)

# Reduce the palette to 8 colors to reduce file size and control
Expand All @@ -63,7 +66,9 @@ def screenshot_display(display: DisplayConfiguration) -> bytes:
img = img.quantize(kmeans=0, palette=palette_img).convert("L")
img = img.rotate(90, expand=1)

hash = hashlib.sha256(img.tobytes()).hexdigest()

output = io.BytesIO()
img.save(output, "png")

return output.getvalue()
return output.getvalue(), hash
6 changes: 2 additions & 4 deletions inkplate_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ async def get(self, request: Request) -> HTMLResponse:

class DisplayPngEndpoint(HTTPEndpoint):
async def get(self, request: Request) -> PlainTextResponse:
return PlainTextResponse(
await run_in_threadpool(screenshot_display, request.app.display),
media_type="image/png",
)
image, _hash = await run_in_threadpool(screenshot_display)
return PlainTextResponse(image, media_type="image/png")

0 comments on commit e152fe7

Please sign in to comment.