-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
214 lines (193 loc) · 5.69 KB
/
Copy pathindex.ts
File metadata and controls
214 lines (193 loc) · 5.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#! /usr/bin/env node
import { Command } from "commander";
import {
removeBackground,
removeForeground,
} from "@imgly/background-removal-node";
import logUpdate from "log-update";
import fs from "fs";
import path from "path";
import { VERSION } from "./version";
// Get the directory where this script is located
const getPublicPath = () => {
try {
// For CommonJS (compiled)
if (typeof __dirname !== "undefined") {
const distPath = path.join(
__dirname,
"..",
"node_modules",
"@imgly",
"background-removal-node",
"dist"
);
// Convert to file:// URI
return `file://${distPath}/`;
}
} catch (e) {
// Fallback
}
// Default fallback
return undefined;
};
const publicPath = getPublicPath();
const program = new Command();
program
.name("background-removal-js-node-cli")
.description("CLI tool for background and foreground removal")
.version(VERSION);
type RmbgOptions = {
input: string;
output: string;
model: "small" | "medium" | "large";
format: "png" | "jpeg" | "x-alpha8" | "x-rgba8" | "webp";
quality: string;
debug: boolean;
};
program
.command("rmbg")
.description("Remove background from an image")
.requiredOption("-i, --input <path>", "Input image path")
.requiredOption("-o, --output <path>", "Output image path")
// small | medium | large
.option(
"-m, --model <model>",
"Model to use for background removal (small, medium, large)",
"medium"
)
.option(
"-f, --format <format>",
"Output image format (png, jpeg, x-alpha8, x-rgba8, webp)",
"png"
)
.option(
"-q, --quality <quality>",
"Output image quality (only for jpeg and webp, 0-100)",
"80"
)
.option(
"-d, --debug",
"Enable debug mode to save intermediate results",
false
)
.action(async (options: RmbgOptions) => {
const inputBuffer = fs.readFileSync(options.input);
// Determine MIME type from file extension
const ext = path.extname(options.input).toLowerCase();
const mimeTypes: Record<string, string> = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".webp": "image/webp",
};
const mimeType = mimeTypes[ext] || "image/jpeg";
const inputImage = new Blob([inputBuffer], { type: mimeType });
const frames = ["-", "\\", "|", "/"];
let index = 0;
let currentProcess = 0;
let totalProcess = 0;
let currentKey = "";
setInterval(() => {
const frame = frames[(index = ++index % frames.length)];
if (totalProcess === 0) return logUpdate(`${frame} Initializing...`);
logUpdate(
`${frame} ${currentKey}: ${Math.round(
(currentProcess / totalProcess) * 100
)}%`
);
}, 80);
const outputImage = await removeBackground(inputImage, {
publicPath,
model: options.model,
output: {
format: ["image", options.format as "png"].join("/") as
| "image/png"
| "image/jpeg",
quality: parseInt(options.quality) / 100,
},
debug: options.debug,
progress(key, current, total, ...args_3) {
currentProcess = current;
totalProcess = total;
currentKey = key;
},
});
const buffer = await outputImage.arrayBuffer();
fs.writeFileSync(options.output, new Uint8Array(buffer));
console.log(`Background removed and saved to ${options.output}`);
process.exit(0);
});
program
.command("rmfg")
.description("Remove forground from an image")
.requiredOption("-i, --input <path>", "Input image path")
.requiredOption("-o, --output <path>", "Output image path")
// small | medium | large
.option(
"-m, --model <model>",
"Model to use for background removal (small, medium, large)",
"medium"
)
.option(
"-f, --format <format>",
"Output image format (png, jpeg, x-alpha8, x-rgba8, webp)",
"png"
)
.option(
"-q, --quality <quality>",
"Output image quality (only for jpeg and webp, 0-100)",
"80"
)
.option(
"-d, --debug",
"Enable debug mode to save intermediate results",
false
)
.action(async (options: RmbgOptions) => {
const inputBuffer = fs.readFileSync(options.input);
// Determine MIME type from file extension
const ext = path.extname(options.input).toLowerCase();
const mimeTypes: Record<string, string> = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".webp": "image/webp",
};
const mimeType = mimeTypes[ext] || "image/jpeg";
const inputImage = new Blob([inputBuffer], { type: mimeType });
const frames = ["-", "\\", "|", "/"];
let index = 0;
let currentProcess = 0;
let totalProcess = 0;
let currentKey = "";
setInterval(() => {
const frame = frames[(index = ++index % frames.length)];
if (totalProcess === 0) return logUpdate(`${frame} Initializing...`);
logUpdate(
`${frame} ${currentKey}: ${Math.round(
(currentProcess / totalProcess) * 100
)}%`
);
}, 80);
const outputImage = await removeForeground(inputImage, {
publicPath,
model: options.model,
output: {
format: ["image", options.format as "png"].join("/") as
| "image/png"
| "image/jpeg",
quality: parseInt(options.quality) / 100,
},
debug: options.debug,
progress(key, current, total, ...args_3) {
currentProcess = current;
totalProcess = total;
currentKey = key;
},
});
const buffer = await outputImage.arrayBuffer();
fs.writeFileSync(options.output, new Uint8Array(buffer));
console.log(`Background removed and saved to ${options.output}`);
process.exit(0);
});
program.parse(process.argv);