NodeJS bindings / wrapper for using libw2xc from waifu2x-converter-cpp
Used to upscale photos or Anime-style art using convolutional neural networks.
This module currently only supports GNU/Linux and Windows.
This project requires node-gyp to build, make sure it is installed using npm install -g node-gyp
Make sure you have node-gyp setup correctly. You'll need Visual Studio 2015 or later installed to compile the source. See https://www.npmjs.com/package/node-gyp#on-windows for more info.
Dependencies are installed automatically but require 7z to extract the binaries, make sure it is installed at the default install path C:\Program Files\7-Zip\7z.exe
The install scripts should install all dependencies automatically so no additional setup is required.
Install the dependencies listed below.
Make sure the you install it in one of the linker's search directories. It should be by default if you use your package manager or follow the build instructions below.
Install OpenCV using your distrobution's package manager.
On arch you'll use pacman -S opencv
-
AUR (ArchLinux based distros)
-
Fedora
-
Other Linux
- Build from source. See instructions here https://github.com/DeadSix27/waifu2x-converter-cpp/blob/master/BUILDING.md
Install using npm
npm install waifu2x-node
import { W2XCJS, DEFAULT_MODELS_DIR } from 'waifu2x-node';
const converter = new W2XCJS();
const err = converter.loadModels(DEFAULT_MODELS_DIR);
if (!err) {
const conv_err = converter.convertFile("in.png", "out.webp");
if (!err) {
console.log('File converted successfully');
}
}
import { W2XCJS, DEFAULT_MODELS_DIR } from 'waifu2x-node';
import fs from 'fs';
const converter = new W2XCJS();
const err = converter.loadModels(DEFAULT_MODELS_DIR);
if (!err) {
const input_buffer = fs.readFileSync("in.png");
const output_buffer = converter.convertBuffer(input_buffer, '.JPG'); // second parameter is the file extension to encode to.
fs.writeFileSync("out.jpg", output_buffer);
}
Asynchronous functions only work on GPU processor types due to instabilities on the CPU
import { W2XCJS, DEFAULT_MODELS_DIR } from 'waifu2x-node';
import fs from 'fs';
const converter = new W2XCJS();
const err = converter.loadModels(DEFAULT_MODELS_DIR); // model loading is synchronous
if (!err) {
fs.readFile("in.png", (err, input_buffer) => {
if (err) throw err;
converter.convertBufferAsync(input_buffer, '.WEBP', { /* AsyncOptions */ }, dst_buffer => {
fs.writeFile("out.webp", dst_buffer, err => {
if (err) throw err;
})
})
});
}
The library provides a wrapper class for using promises
import { W2XCJS, DEFAULT_MODELS_DIR, W2XCJSPromises } from 'waifu2x-node';
import fs from 'fs';
const promises = new W2XCJSPromises(new W2XCJS());
const err = promises.converter.loadModels(DEFAULT_MODELS_DIR); // model loading is synchronous
if (!err) {
(async () => {
const input_buffer = await fs.promises.readFile("in.png");
const dst_buffer = await promises.convertBuffer(input_buffer, '.WEBP', { /* AsyncOptions */ });
await fs.promises.writeFile("out.webp", dst_buffer);
})();
}
Abstract of the library source for reference, you could also generate the documentation for more detailed overview.
interface AsyncOptions {
// encoding options for destination buffer.
imwrite_params: ImwriteParams;
// denoising options (number value from -1 to 3 where -1 is no denoising)
denoise_level: DenoiseLevel;
// Scale factor.
scale: number;
}
interface ImwriteParams {
// quality factor for webp and jpeg from 0 to 101 where 101 is lossless.
webp_quality?: number;
jpeg_quality?: number;
// compression factor for png from 0 to 9 where 9 is smallest size and longest time.
png_compression?: number;
}
Documentation is generated using TypeDoc, run npm run docs:build
to build the documentation and npm run docs:serve
to serve a local copy of the documentation.