Skip to content

The swiss army knife of lossless video/audio editing

releaseImage: fixes from review

Claude8a8c12a6edcd
diff --git a/script/releaseImage.ts b/script/releaseImage.ts
index 36707322..3ab836b4 100644
--- a/script/releaseImage.ts
+++ b/script/releaseImage.ts
@@ -1,6 +1,7 @@
// Generates an image for release announcements, see AGENTS.md "Release announcement"
-// Usage: node script/releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|left|right|...] [--caption "photo caption"] [--square] [--out file.png] "highlight 1" "highlight 2" ...
import assert from 'node:assert';
+import { mkdir } from 'node:fs/promises';
+import { dirname } from 'node:path';
import { parseArgs } from 'node:util';
import sharp from 'sharp';
@@ -8,7 +9,8 @@ const { values: { bg, position, caption, square, out }, positionals: [version, .
allowPositionals: true,
options: { bg: { type: 'string' }, position: { type: 'string' }, caption: { type: 'string' }, square: { type: 'boolean' }, out: { type: 'string' } },
});
-assert(version != null && highlights.length <= 3, 'Usage: releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|...] [--caption "photo caption"] [--square] [--out file.png] [up to 3 highlights]');
+const usage = 'Usage: node script/releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|left|right|center] [--caption "photo caption"] [--square] [--out file.png] [up to 3 "highlights"]';
+assert(version != null && highlights.length <= 3, usage);
// 16:9 for X/Bluesky/Mastodon/Discord, or square for Instagram
const width = square ? 1080 : 1600;
@@ -16,14 +18,19 @@ const height = square ? 1080 : 900;
const margin = square ? 80 : 110;
const scale = square ? 0.8 : 1;
+// Text doesn't wrap, so warn about text that probably runs off the image
+const maxHighlightLength = square ? 40 : 50;
+highlights.filter((highlight) => highlight.length > maxHighlightLength).forEach((highlight) => console.warn(`Highlight may be too long (${highlight.length} > ${maxHighlightLength} chars):`, highlight));
+if (caption != null && caption.length > 60) console.warn('Caption may be too long:', caption);
+
const escapeXml = (str: string) => str.replaceAll(/[<>&'"]/g, (c) => `&#${c.codePointAt(0)};`);
const svg = (content: string) => Buffer.from(`<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">${content}</svg>`);
// Crop the photo (e.g. a portrait) to the image's aspect ratio. By default keep the most interesting part (sharp's attention strategy), or the part given by --position (e.g. top, bottom, left, right, center)
-const photo = bg != null ? await sharp(bg).rotate().resize(width, height, { fit: 'cover', position: position ?? sharp.strategy.attention }).toBuffer() : undefined;
+const photo = bg != null ? await sharp(bg).rotate().resize(width, height, { fit: 'cover', position: position ?? sharp.strategy.attention }).flatten({ background: '#000' }).toBuffer() : undefined;
// Accent color: the photo's dominant color, or else a hue derived from the version, so every release looks a bit different
-async function getAccentHue() {
+async function getAccentHue(seed: string) {
if (photo != null) {
const { dominant: { r, g, b } } = await sharp(photo).stats();
const max = Math.max(r, g, b);
@@ -35,9 +42,9 @@ async function getAccentHue() {
return Math.round((h * 60 + 360) % 360);
}
}
- return [...version!].reduce((acc, c) => (acc * 31 + c.codePointAt(0)!) % 360, 7);
+ return [...seed].reduce((acc, c) => (acc * 31 + c.codePointAt(0)!) % 360, 7);
}
-const hue = await getAccentHue();
+const hue = await getAccentHue(version);
const accent = `hsl(${hue}, 80%, 65%)`;
// Darken the text area (left side) enough for the text to stay readable, also on bright photos
@@ -49,8 +56,8 @@ async function getTextAreaBrightness() {
return channels[0]!.mean;
}
const brightness = await getTextAreaBrightness();
-// Opacity that brings the area's mean brightness down to about 40 (of 255)
-const shadeOpacity = Math.min(0.95, Math.max(0.7, 1 - 40 / Math.max(brightness, 1)));
+// At least 0.7, more for bright photos, so the area ends up dark (roughly 40-55 of 255)
+const shadeOpacity = Math.max(0.7, 1 - 40 / brightness);
// Darkens (and blurs) the left side, fading out towards the right
const fade = (id: string, stops: [number, number][]) => `<linearGradient id="${id}" x1="0" x2="1" y1="0" y2="0">${stops.map(([offset, opacity]) => `<stop offset="${offset}" stop-color="hsl(${hue}, 60%, 7%)" stop-opacity="${opacity}" />`).join('')}</linearGradient>`;
@@ -85,6 +92,7 @@ async function getBackground() {
const icon = await sharp('src/renderer/src/icon.svg').resize(iconSize, iconSize).png().toBuffer();
const outPath = out ?? `dist/release-${version}${square ? '-square' : ''}.png`;
+await mkdir(dirname(outPath), { recursive: true });
await sharp(await getBackground())
.composite([{ input: overlay }, { input: icon, left: margin, top }])
.png()