Skip to content

Commit

Permalink
Allow switching output image format
Browse files Browse the repository at this point in the history
  • Loading branch information
nbarrientos committed Mar 15, 2024
1 parent 5380aad commit d99821d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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. |

Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 3 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)?"
Expand All @@ -62,4 +64,4 @@ schema:
- name: match(^[A-Z0-9_]+$)
value: str
environment:
output_path: "/output/cover.png"
output_path: "/output/cover"
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down Expand Up @@ -165,15 +166,15 @@ 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";

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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit d99821d

Please sign in to comment.