Created
January 27, 2026 21:24
-
-
Save jide/193f3a1026c26b32081735cb469be0fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env npx tsx | |
| /** | |
| * Extract Alpha (Atomic Tool) | |
| * | |
| * Performs alpha matting using white and black isolation images. | |
| * Pure image processing - no AI, no verification needed. | |
| * | |
| * Usage: | |
| * npx tsx extract-alpha.ts <screen-dir> | |
| * | |
| * Requires: | |
| * - ui_white.png (white isolation) | |
| * - ui_black.png (black isolation) | |
| * | |
| * Output (JSON to stdout): | |
| * { | |
| * "success": true, | |
| * "outputPath": "/path/to/assets/alpha.png" | |
| * } | |
| * | |
| * Exit codes: | |
| * 0 = success | |
| * 2 = error | |
| */ | |
| import path from "path"; | |
| import fs from "fs"; | |
| import { | |
| loadEnv, | |
| extractAlphaTwoPass, | |
| } from "../lib/index.js"; | |
| loadEnv(); | |
| interface ExtractResult { | |
| success: boolean; | |
| outputPath: string; | |
| error?: string; | |
| } | |
| function parseArgs() { | |
| const args = process.argv.slice(2); | |
| if (args.length < 1) { | |
| console.error("Usage: extract-alpha.ts <screen-dir>"); | |
| process.exit(2); | |
| } | |
| const screenDir = path.resolve(args[0]); | |
| return { screenDir }; | |
| } | |
| async function main() { | |
| const { screenDir } = parseArgs(); | |
| // Check required files exist | |
| const whitePath = path.join(screenDir, "ui_white.png"); | |
| const blackPath = path.join(screenDir, "ui_black.png"); | |
| for (const p of [whitePath, blackPath]) { | |
| if (!fs.existsSync(p)) { | |
| const result: ExtractResult = { | |
| success: false, | |
| outputPath: "", | |
| error: `Required file not found: ${p}`, | |
| }; | |
| console.log(JSON.stringify(result, null, 2)); | |
| process.exit(2); | |
| } | |
| } | |
| // Ensure assets directory exists | |
| const assetsDir = path.join(screenDir, "assets"); | |
| if (!fs.existsSync(assetsDir)) { | |
| fs.mkdirSync(assetsDir, { recursive: true }); | |
| } | |
| const outputPath = path.join(assetsDir, "alpha.png"); | |
| // Run alpha extraction | |
| try { | |
| await extractAlphaTwoPass(whitePath, blackPath, outputPath); | |
| } catch (e: any) { | |
| const result: ExtractResult = { | |
| success: false, | |
| outputPath: "", | |
| error: `Alpha extraction failed: ${e.message}`, | |
| }; | |
| console.log(JSON.stringify(result, null, 2)); | |
| process.exit(2); | |
| } | |
| // Verify output exists | |
| if (!fs.existsSync(outputPath)) { | |
| const result: ExtractResult = { | |
| success: false, | |
| outputPath: "", | |
| error: "Alpha extraction did not produce output file", | |
| }; | |
| console.log(JSON.stringify(result, null, 2)); | |
| process.exit(2); | |
| } | |
| const result: ExtractResult = { | |
| success: true, | |
| outputPath, | |
| }; | |
| console.log(JSON.stringify(result, null, 2)); | |
| process.exit(0); | |
| } | |
| main().catch((err) => { | |
| console.error(JSON.stringify({ success: false, error: String(err) })); | |
| process.exit(2); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment