-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (32 loc) · 1.21 KB
/
index.ts
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
import * as fs from "fs";
import * as path from "path";
import { transparentBackground } from "transparent-background";
const imagesDir = path.join(__dirname, "images");
const processedDir = path.join(__dirname, "processed");
const removeBackground = async (filename: string) => {
const input = await fs.promises.readFile(path.join(imagesDir, filename));
const output = await transparentBackground(input, "png", {
fast: false, // uses a 1024x1024 model by default, setting to true uses a 384x384 model instead (faster but lower quality).
});
await fs.promises.writeFile(path.join(processedDir, filename), output);
await fs.promises.unlink(path.join(imagesDir, filename));
console.log(`Processed ${filename}`);
};
const start = Date.now();
console.log(`Getting the model and processing all images in ${imagesDir}\n`);
if (!fs.existsSync(processedDir)) {
fs.mkdirSync(processedDir);
}
Promise.all(
fs
.readdirSync(imagesDir)
.filter((filename) => {
return filename.endsWith(".png") || filename.endsWith(".jpg");
})
.map((filename) => {
return removeBackground(filename);
})
).then(() => {
const end = Date.now();
console.log(`\nProcessed all images in ${end - start}ms`);
});