forked from EddyVerbruggen/remove.bg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveFromBase64.ts
More file actions
32 lines (27 loc) · 1.06 KB
/
removeFromBase64.ts
File metadata and controls
32 lines (27 loc) · 1.06 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
import * as fs from "fs";
import { RemoveBgError, removeBackgroundFromImageBase64 } from "remove.bg";
const apiKeyFile = require("./_test_apikey.json");
if (!apiKeyFile || !apiKeyFile.apiKey) {
throw new Error("No apikey found");
}
async function removeBgFromBase64(path) {
try {
const outputFile = `${__dirname}/out/img-removed-from-base64.png`;
const base64img = fs.readFileSync(path, { encoding: "base64" });
const result = await removeBackgroundFromImageBase64({
base64img,
apiKey: apiKeyFile.apiKey,
size: "regular",
outputFile
});
console.log(`File saved to ${outputFile}`);
console.log(`${result.creditsCharged} credit(s) charged for this image`);
console.log(`Result width x height: ${result.resultWidth} x ${result.resultHeight}, type: ${result.detectedType}`);
console.log(result.base64img.substring(0, 40) + "..");
} catch (e) {
const errors: Array<RemoveBgError> = e;
console.log("Error: " + JSON.stringify(errors));
}
return null;
}
removeBgFromBase64("./examples/testfiles/trump-small.jpg");