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

image-pixelizer

v1.1.0

Published

A tool to generate pixel arts from images

Downloads

289

Readme

image-pixelizer

A tool to generate pixel arts from images.

Release Notes

Version 1.1.0

  • Stable RGBA -> XYZA -> LAB(alpha) conversion.
  • Improved color difference performance.

Version 1.0.7

  • Fixed a minor bug in distribution version of the vanila Javascript libraies.

Version 1.0.6

  • Added RGBA to LAB (through XYZ) color conversion for more accurate color comparison.
  • Added support for vanila Javascript use case: image-pixelizer.min.js.

Algorithm

This tool uses a simple two-stage algorithm to create pixel arts from images.

  1. Pixel clustering: Cluster pixels based on their similarity in color and relative distance.
  2. Color Reduction: Reduce the number of colors of the clustered image to maximize color diversity.

Install

npm install image-pixelizer --save

Usage

// Import Pixelizer.
const Pixelizer = require('image-pixelizer');

// Create Options for Pixelizer.
let options = new Pixelizer.Options()
    .setPixelSize(1)
    .setColorDistRatio(0.5)
    .setClusterThreshold(0.01)
    .setMaxIteration(10)
    .setNumberOfColors(128);

// Create compatible input Bitmap.
let inputBitmap = new Pixelizer.Bitmap(
    1920,   // width in pixels
    1080,   // height in pixels
    [...]   // one dimension data array for RGBA bitmap
);

// Pixelize!
let outputBitmap = 
    new Pixelizer(inputBitmap, options).pixelize();

Options

There are in total 5 options for the Pixelizer.

  1. Pixel Size: This is size of output bitmap pixel in terms of the input bitmap pixels. For example, if this number is 10, each output bitmap pixel represents a 10x10 square (100 pixels) in the input bitmap.
  2. Color Distance Ratio: This is a 0 to 1 factor which tunes the weights assigned to pixel color difference and distance difference during clustering. 0 means only using distance difference when deciding which cluster a pixel goes to, while 1 means only use the color difference.
  3. Cluster Threshold: This is the ratio between number of pixels that changed cluster and number of total pixels, which is used as the primary condition to stop clustering. For example, if this number is 0.01, it means algorithm will stop clustering if less than 1% of pixels changed cluster assignment during the last iteration.
  4. Maximum Iteration: This is the maximum number of iterations allowed during pixel clustering stage. This is set as a hard stop condition to avoid clustering that does not converge.
  5. Number of Colors: This is the intended number of colors in the output bitmap.

Default values:

  • pixelSize = 1
  • colorDistRatio = 0.5
  • clusterThreshold = 0.01
  • maxIteration = 10
  • numberOfColors = 128

Bitmap

Bitmap is a light-weight wrapper class to represent a RGBA bitmap image. It's created with width, height (both in pixels) and a one dimensional data array containing RGBA values for all pixels. Each RGBA value should be a number between 0 to 255.

An extremely simple example which has 4 pixels:

|------------------------|-----------------------|
| black (0, 0, 0, 255)   | red (255, 0, 0, 255)  |
|------------------------|-----------------------|
| green (0, 255, 0, 255) | blue (0, 0, 255, 255) |
|------------------------|-----------------------|

To create this bitmap:

let bitmap = new Pixelizer.Bitmap(
    2, 
    2,
    [
        0,   0,   0,   255,     // black
        255, 0,   0,   255,     // red
        0,   255, 0,   255,     // green
        0,   0,   255, 255      // blue
    ]
);

If you use jimp, you might notice this shares similarity with its bitmap. This is by design, which means you can create Pixelizer Bitmap directly from jimp, and use jimp to output image file by overriding its bitmap:

Jimp.read('lenna.png')
  .then(lenna => {
      // Create Pixelizer bitmap from jimp.
      let inputBitmap = new Pixelizer.Bitmap(
          lenna.bitmap.width,
          lenna.bitmap.height,
          lenna.bitmap.data
      );
      // Pixelizer processing code...
      let outputBitmap = ...
      // Override jimp bitmap and output image.
      lenna.bitmap.width = outputBitmap.width;
      lenna.bitmap.height = outputBitmap.height;
      lenna.bitmap.data = outputBitmap.data;
      lenna.write('lenna-pixel.png');
  })
  .catch(err => {
    console.error(err);
  });

As you can see, there is no deep integration with jimp for the Bitmap class, so this package can be used in more use cases (of course, with the help of more adapter code for Bitmap).