I tried a simple example for node. But I found the default publicPath is always resolved incorrect.
The default publicPath is "file://D:/code\\node\\removeBackground_example\\node_modules\\@imgly\\background-removal-node\\dist/" on my windows. And the url.pathname is '/D:/code/node/removeBackground_example/node_modules/@imgly/background-removal-node/dist/resources.json'. When this path passed to fs.readFile, it produced ENOENT: no such file or directory error.
// index.mjs
import { removeBackground} from "@imgly/background-removal-node"
import { writeFile } from 'node:fs/promises';
import { Buffer } from 'node:buffer';
const public_path = "file:///code/node/removeBackground_example/node_modules/@imgly/background-removal-node/dist/" ; // the path on the local file system
let config = {
debug: true,
publicPath: public_path, // path to the wasm files
progress: (key, current, total) => {
console.log(`Downloading ${key}: ${current} of ${total}`);
}
};
let image_src = 'file://D:/code/node/removeBackground_example/example.jpg';
const blob = await removeBackground(image_src, config);
// result is a blob encoded as PNG.
// It can be converted to an URL to be used as HTMLImage.src
// save blob as result.png
console.info(`blob type: ${typeof blob}`)
const data = Buffer.from(await blob.arrayBuffer());
console.info(`data type: ${typeof data}`)
await writeFile('result.png', data);
console.log('The file has been saved!');
I can change let image_src = 'file://D:/code/node/removeBackground_example/example.jpg'; to let image_src = './example.jpg'; in the above example code to simplify the relative URL path.
I tried a simple example for node. But I found the default
publicPath
is always resolved incorrect.The default
publicPath
is"file://D:/code\\node\\removeBackground_example\\node_modules\\@imgly\\background-removal-node\\dist/"
on my windows. And the url.pathname is'/D:/code/node/removeBackground_example/node_modules/@imgly/background-removal-node/dist/resources.json'
. When this path passed to fs.readFile, it producedENOENT: no such file or directory
error.According to readFile docs on https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options, the file argument accepts URL type, so the fix is simple, just replace
readFile(uri.pathname)
toreadFile(uri)
.See https://github.com/imgly/background-removal-js/blob/32b4fc0843f13e344aacce54529eb6491ea8c6f0/packages/node/src/schema.ts#L21 https://github.com/imgly/background-removal-js/blob/32b4fc0843f13e344aacce54529eb6491ea8c6f0/packages/node/src/resource.ts#L21
workable example code without fix: