-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit-reference.js
More file actions
91 lines (73 loc) · 2.54 KB
/
split-reference.js
File metadata and controls
91 lines (73 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const referenceDir = path.join(__dirname, "../reference_screenshots");
const referencePath = path.join(referenceDir, "notion_reference.png");
if (!fs.existsSync(referencePath)) {
console.error("Reference screenshot not found:", referencePath);
process.exit(1);
}
// Create split directory
const splitDir = path.join(referenceDir, "split");
if (!fs.existsSync(splitDir)) {
fs.mkdirSync(splitDir, { recursive: true });
}
async function splitReference() {
console.log("Splitting reference screenshot into 16:9 segments...");
// First, get image dimensions using imagemagick identify
const identify = spawn("identify", ["-format", "%wx%h", referencePath]);
let dimensions = "";
identify.stdout.on("data", (data) => {
dimensions += data.toString();
});
await new Promise((resolve, reject) => {
identify.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Failed to get image dimensions: ${code}`));
}
});
});
const [width, height] = dimensions.trim().split("x").map(Number);
console.log(`Reference image dimensions: ${width}x${height}`);
// Calculate 16:9 aspect ratio segments
const segmentWidth = 1200; // Standard width
const segmentHeight = Math.floor(segmentWidth * (9 / 16)); // 675px for 16:9 ratio
const segments = Math.ceil(height / segmentHeight);
console.log(
`Creating ${segments} segments with 16:9 aspect ratio (${segmentWidth}x${segmentHeight})`
);
for (let i = 0; i < segments; i++) {
const startY = i * segmentHeight;
const actualHeight = Math.min(segmentHeight, height - startY);
console.log(
`Creating segment ${i + 1}/${segments} (y: ${startY}, height: ${actualHeight})`
);
const outputPath = path.join(
splitDir,
`reference_segment_${String(i + 1).padStart(2, "0")}.png`
);
// Use imagemagick convert to crop the image
const convert = spawn("convert", [
referencePath,
"-crop",
`${segmentWidth}x${actualHeight}+0+${startY}`,
"+repage",
outputPath,
]);
await new Promise((resolve, reject) => {
convert.on("close", (code) => {
if (code === 0) {
console.log(`Saved: ${outputPath}`);
resolve();
} else {
reject(new Error(`Failed to create segment ${i + 1}: ${code}`));
}
});
});
}
console.log("Reference screenshot split completed");
}
splitReference().catch(console.error);