From 24b1a8cba76931541244a07ac6db72dc38c44505 Mon Sep 17 00:00:00 2001 From: Nacho Barrientos Date: Fri, 15 Mar 2024 14:08:13 +0100 Subject: [PATCH 1/2] Allow switching output image format --- README.md | 5 +++-- config.js | 3 ++- config.yaml | 4 +++- docker-compose.yml | 2 +- index.js | 13 +++++++------ run.sh | 1 + 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6f53342..6f85d60 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ If you're looking for a way to render your own HTML, see my other project [hass- ## Features -This tool regularly takes a screenshot of a specific page of your home assistant setup. It converts it into the PNG grayscale format which Kindles can display. +This tool regularly takes a screenshot of a specific page of your home assistant setup. It converts it into the PNG/JPEG grayscale format which Kindles can display. Using my [own Kindle 4 setup guide](https://github.com/sibbl/hass-lovelace-kindle-4) or the [online screensaver extension](https://www.mobileread.com/forums/showthread.php?t=236104) for any jailbroken Kindle, this image can be regularly polled from your device so you can use it as a weather station, a display for next public transport departures etc. @@ -47,6 +47,7 @@ Home Assistant related stuff: | `SCALING` | `1` | no | yes | Scaling factor, e.g. `1.5` to zoom in or `0.75` to zoom out | | `GRAYSCALE_DEPTH` | `8` | no | yes | Grayscale bit depth your kindle supports | | `COLOR_MODE` | `GrayScale` | no | yes | ColorMode to use, ex: `GrayScale`, or `TrueColor`. | +| `IMAGE_FORMAT` | `png` | no | no | Format for the generated images. Acceptable values are `png` or `jpeg`. | | `DITHER` | `false` | no | yes | Apply a dither to the images. | | `REMOVE_GAMMA` | `true` | no | no | Remove gamma correction from image. Computer images are normally gamma corrected since monitors expect gamma corrected data, however some E-Ink displays expect images not to have gamma correction. | @@ -109,7 +110,7 @@ Modify the following lines in the HASS Lovelace Kindle 4 extension's [`script.sh Some advanced variables for local usage which shouldn't be necessary when using Docker: -- `OUTPUT_PATH=./output.png` (destination of rendered image. `OUTPUT_2`, `OUTPUT_3`, ... is also supported) +- `OUTPUT_PATH=./output` (destination of rendered image, without extension. `OUTPUT_2`, `OUTPUT_3`, ... is also supported) - `PORT=5000` (port of server, which returns the last image) - `USE_IMAGE_MAGICK=false` (use ImageMagick instead of GraphicsMagick) - `UNSAFE_IGNORE_CERTIFICATE_ERRORS=true` (ignore certificate errors of e.g. self-signed certificates at your own risk) diff --git a/config.js b/config.js index 919cfb5..fe7c84d 100644 --- a/config.js +++ b/config.js @@ -13,10 +13,11 @@ function getPagesConfig() { if (!screenShotUrl) return pages; pages.push({ screenShotUrl, + imageFormat: getEnvironmentVariable("IMAGE_FORMAT", suffix) || "png", outputPath: getEnvironmentVariable( "OUTPUT_PATH", suffix, - `output/cover${suffix}.png` + `output/cover${suffix}` ), renderingDelay: getEnvironmentVariable("RENDERING_DELAY", suffix) || 0, renderingScreenSize: { diff --git a/config.yaml b/config.yaml index 508e717..f3c2913 100644 --- a/config.yaml +++ b/config.yaml @@ -35,6 +35,7 @@ options: ROTATION: '0' SCALING: '1' GRAYSCALE_DEPTH: '8' + IMAGE_FORMAT: 'png' COLOR_MODE: 'GrayScale' REMOVE_GAMMA: true PREFERS_COLOR_SCHEME: 'light' @@ -54,6 +55,7 @@ schema: ROTATION: "int?" SCALING: "float?" GRAYSCALE_DEPTH: "int?" + IMAGE_FORMAT: "list(png|jpeg)?" COLOR_MODE: "list(GrayScale|TrueColor)?" REMOVE_GAMMA: "bool?" PREFERS_COLOR_SCHEME: "list(light|dark)?" @@ -62,4 +64,4 @@ schema: - name: match(^[A-Z0-9_]+$) value: str environment: - output_path: "/output/cover.png" + output_path: "/output/cover" diff --git a/docker-compose.yml b/docker-compose.yml index 1da0140..cfcaf63 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: - RENDERING_SCREEN_HEIGHT=800 - RENDERING_SCREEN_WIDTH=600 - GRAYSCALE_DEPTH=8 - - OUTPUT_PATH=/output/cover.png + - OUTPUT_PATH=/output/cover - LANGUAGE=en - ROTATION=0 - SCALING=1 diff --git a/index.js b/index.js index d127e45..a80671a 100644 --- a/index.js +++ b/index.js @@ -105,13 +105,14 @@ const batteryStore = {}; const pageIndex = pageNumber - 1; const configPage = config.pages[pageIndex]; - const data = await fs.readFile(configPage.outputPath); - const stat = await fs.stat(configPage.outputPath); + const outputPathWithExtension = configPage.outputPath + "." + configPage.imageFormat + const data = await fs.readFile(outputPathWithExtension); + const stat = await fs.stat(outputPathWithExtension); const lastModifiedTime = new Date(stat.mtime).toUTCString(); response.writeHead(200, { - "Content-Type": "image/png", + "Content-Type": "image/" + configPage.imageFormat, "Content-Length": Buffer.byteLength(data), "Last-Modified": lastModifiedTime }); @@ -165,7 +166,7 @@ async function renderAndConvertAsync(browser) { const url = `${config.baseUrl}${pageConfig.screenShotUrl}`; - const outputPath = pageConfig.outputPath; + const outputPath = pageConfig.outputPath + "." + pageConfig.imageFormat; await fsExtra.ensureDir(path.dirname(outputPath)); const tempPath = outputPath + ".temp"; @@ -173,7 +174,7 @@ async function renderAndConvertAsync(browser) { console.log(`Rendering ${url} to image...`); await renderUrlToImageAsync(browser, pageConfig, url, tempPath); - console.log(`Converting rendered screenshot of ${url} to grayscale png...`); + console.log(`Converting rendered screenshot of ${url} to grayscale...`); await convertImageToKindleCompatiblePngAsync( pageConfig, tempPath, @@ -278,7 +279,7 @@ async function renderUrlToImageAsync(browser, pageConfig, url, path) { } await page.screenshot({ path, - type: "png", + type: pageConfig.imageFormat, clip: { x: 0, y: 0, diff --git a/run.sh b/run.sh index 9aab4b9..f0bbddc 100755 --- a/run.sh +++ b/run.sh @@ -15,6 +15,7 @@ export BROWSER_LAUNCH_TIMEOUT=$(bashio::config 'BROWSER_LAUNCH_TIMEOUT') export ROTATION=$(bashio::config 'ROTATION') export SCALING=$(bashio::config 'SCALING') export GRAYSCALE_DEPTH=$(bashio::config 'GRAYSCALE_DEPTH') +export IMAGE_FORMAT=$(bashio::config 'IMAGE_FORMAT') export COLOR_MODE=$(bashio::config 'COLOR_MODE') export REMOVE_GAMMA=$(bashio::config 'REMOVE_GAMMA') export PREFERS_COLOR_SCHEME=$(bashio::config 'PREFERS_COLOR_SCHEME') From b8ab75c5bbb081ac216c272e6bf2de841e05808c Mon Sep 17 00:00:00 2001 From: Sebastian Schubotz Date: Fri, 15 Mar 2024 16:41:19 +0100 Subject: [PATCH 2/2] Bump version, update changelog --- CHANGELOG.md | 6 ++++++ config.yaml | 2 +- package.json | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbbdd72..43a1a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.0.9 + +### Added + +* Add jpeg support via new `IMAGE_TYPE` config env variable (thanks to [@nbarrientos](https://github.com/nbarrientos)) + ## 1.0.8 ### Fixed diff --git a/config.yaml b/config.yaml index f3c2913..4c83a83 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ name: Lovelace Kindle Screensaver -version: 1.0.8 +version: 1.0.9 slug: kindle-screensaver description: This tool can be used to display a Lovelace view of your Home Assistant instance on a jailbroken Kindle device. It regularly takes a screenshot which can be polled and used as a screensaver image of the online screensaver plugin. startup: application diff --git a/package.json b/package.json index 528fd3a..b06f942 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hass-lovelace-kindle-screensaver", - "version": "1.0.8", + "version": "1.0.9", "description": "", "main": "index.js", "scripts": {