项目作者: xVanTuring

项目描述 :
Async Node color quantization by native addon
高级语言: JavaScript
项目地址: git://github.com/xVanTuring/cquant.git
创建时间: 2019-01-11T12:33:34Z
项目社区:https://github.com/xVanTuring/cquant

开源协议:Other

下载


CQuant

Build status
Build Status
Npm
cquant.png

Cquant is node-addon-api based node extension, with the performance of C++, you can easily compute the major color of an image. The core algorithm of Cquant is based on leptonica.

CQuant-Web is Here!

CQuant-Web

Usage

View Latest Doc on Github

  • Supported Platform: Windows, Linux, macOS
  • Node Supported Prebuild binary version: 6 | 8 | 10 | 11
  • Electron Prebuild Supported Versions: v3 and v4.0.4

    Install

    npm i cquant sharp // install cquant and sharp

    Async!

    This package is async. You can run multiple task without blocking the main loop.

    Basic

    ``` js
    const cquant = require(‘cquant’)
    // use sharp to convert image to RGB Buffer Array
    const sharp = require(‘sharp’)
    sharp(‘path/to/image’)
    .raw() // convert raw buffer like [RGB RGB RGB RGB]
    .toBuffer((_err, buffer, info) => {
    if (!_err) {

    1. let colorCount = 4
    1. cquant.paletteAsync(buffer, info.channels, colorCount).then(res => {
    2. console.log(res)
    3. }).catch(err => {
    4. console.log(err)
    5. })

    }
    })
    ```

    API

    ``` ts
    /**

    • @param buffer Image Buffer(RGB/RGBA)
    • @param depth 3 or 4 for RGB/RGBA
    • @param maxColor Color Amount You want
    • @param maxSub max sub-sample for image, 1 for no sub sample,0 for auto, by default it will scale to size of 1000x1000
    • @param callback callback with err and result
      /
      function paletteAsync(buffer: Buffer, depth=3, maxColor=5, maxSub=0, callback:CallBackFunc): void;
      interface Color {
      R: number; /
      red/
      G: number; /
      green/
      B: number; /
      blue/
      count: number; /
      count*/
      }
      declare type Palette = Color[];
      type CallBackFunc = (err, result: Palette) => void;
      function paletteAsync(buffer: Buffer, depth=3, maxColor=5, maxSub=0): Promise;
  1. ## Perf
  2. > test result will be diff based on your local machine
  3. ### JPG 5572 x 3715 (No SubSample)
  4. | Program | Time(ms) |
  5. |---------------|:--------:|
  6. | cquant | 60 ms |
  7. | image-palette | N/A |
  8. > N/A: crashed
  9. ### JPG 1920 x 1280 (No SubSample)
  10. | Program | Time(ms) |
  11. |---------------|:--------:|
  12. | cquant | 12ms |
  13. | image-palette | 950ms |
  14. ## Extra
  15. ### With `async.queue`
  16. > If you have lots of image to process, the best way to do it is using [async](https://www.npmjs.com/package/async).queue for parallel, and controllable
  17. ``` js
  18. // test/example.js
  19. const myQueue = async.queue(async (filePath) => {
  20. const img = await sharp(filePath)
  21. .raw() // to raw
  22. .toBuffer({ resolveWithObject: true })
  23. const palette = await cquant.paletteAsync(img.data, img.info.channels, 5)
  24. console.log(palette)
  25. }, os.cpus().length - 1)

Preview

Screenshot from 2019-02-09 15-16-32.png

Electron User

After running the install command make sure to use electron-rebuild to rebuild it for electron, usually it will just download the prebuild.

Build Your Self

Build Tool

To be able to build from the source, you also need the standard build tool based on your OS.

For Windows User

  • You can use this awesome app windows-build-tools to auto download all tools needed, if you don’t have visual studio installed
  • If you have already installed a visual studio like vs2017, basically you just need to enable c++ development(it’s gonna be huge).

    Build it

    Basically you need run this command
    1. npm run build

xVan Turing 2019