npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cquant

v0.1.5

Published

A fast and native image palette generator

Downloads

40

Readme

CQuant

Build status Build Status Npm

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.

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