forked from EddyVerbruggen/remove.bg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveFromFileWithCropAndScale.ts
More file actions
32 lines (28 loc) · 1.07 KB
/
removeFromFileWithCropAndScale.ts
File metadata and controls
32 lines (28 loc) · 1.07 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 { 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-with-crop-and-scale.png`;
removeBackgroundFromImageFile({
path,
outputFile,
apiKey: apiKeyFile.apiKey,
size: "regular",
type: "person",
crop: true,
crop_margin: "30px",
scale: "70%"
}).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-small.jpg");