forked from EddyVerbruggen/remove.bg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveFromFile.ts
More file actions
29 lines (25 loc) · 1015 Bytes
/
removeFromFile.ts
File metadata and controls
29 lines (25 loc) · 1015 Bytes
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
import { removeBackgroundFromImageFile, RemoveBgError } from "remove.bg";
import { RemoveBgResult } from "../index";
const apiKeyFile = require("./_test_apikey.json");
if (!apiKeyFile || !apiKeyFile.apiKey) {
throw new Error("No apikey found");
}
async function removeBgFromLocalFile(path) {
const outputFile = `${__dirname}/out/img-removed-from-file.png`;
removeBackgroundFromImageFile({
path,
apiKey: apiKeyFile.apiKey,
size: "regular",
type: "person",
outputFile
}).then((result: RemoveBgResult) => {
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((errors: Array<RemoveBgError>) => {
console.log(JSON.stringify(errors));
});
return null;
}
removeBgFromLocalFile("./examples/testfiles/trump-large.jpg");