Git Product home page Git Product logo

cquant's Introduction

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

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) {
      let colorCount = 4

      cquant.paletteAsync(buffer, info.channels, colorCount).then(res => {

        console.log(res)
      }).catch(err => {

        console.log(err)
      })
    }
  })

API

  /**
   * 
   * @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<Palette>;
  

Perf

test result will be diff based on your local machine

JPG 5572 x 3715 (No SubSample)

Program Time(ms)
cquant 60 ms
image-palette N/A

N/A: crashed

JPG 1920 x 1280 (No SubSample)

Program Time(ms)
cquant 12ms
image-palette 950ms

Extra

With async.queue

If you have lots of image to process, the best way to do it is using async.queue for parallel, and controllable

// test/example.js
const myQueue = async.queue(async (filePath) => {
  const img = await sharp(filePath)
    .raw() // to raw
    .toBuffer({ resolveWithObject: true })
  const palette = await cquant.paletteAsync(img.data, img.info.channels, 5)
  console.log(palette)
}, 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

npm run build

xVan Turing 2019

cquant's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cquant's Issues

About sharp

First of all, great work ! It is indeed super fast, even on a Raspberry Pi.

I have just a small observation : you rely on sharp as it is noted in the basic example, but it’s not part of the dependencies. So the example produces en error (Error: Cannot find module ‘sharp’).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.