Git Product home page Git Product logo

node-pixelr's Introduction

About

Simple pixel reader, supporting JPEG and PNG formats.

Example

This is how I ended up using pixelr in my project to create video chatting in the terminal (https://github.com/billyeh/termchat). I read pixels from an image, compress them, and match them to colors to form a string I could output to the terminal.

var pixelr = require('pixelr')
  , colors = require('./node_modules/blessed/lib/colors');
// Colors.js contains a function to match RGB colors to terminal characters.
// It's from the awesome library Blessed: https://github.com/chjj/blessed

/** pixelr.read(filename, format, callback)
 * Format can be either 'jpeg' or 'png'.
 * This function creates a one-dimensional array of values.
 * PNGs have 4 values per pixel: R, G, B, Alpha
 * JPEGs have 3 values per pixel: R, G, B
 * The callback takes an object with properties 'pixels' (an array), 'width', and 'height'.
 */
pixelr.read("image.jpeg", "jpeg", asciizeImage);

function asciizeImage(err, image) {
  if (err) { console.log(err); return; }
  return asciize(image, 400, 400);
}

function asciize(image, asciiWidth, asciiHeight) {
  var countedColors = countColors(image, asciiWidth, asciiHeight)
    , ascii = ''
    , ansiColorCode
    , previousColor = -2;

  for (var i = 0; i < countedColors.length; i++) {
    ansiColorCode = sortColors(countedColors[i])[0].color;
    if (ansiColorCode !== previousColor) {
      ascii += '\033[38;5;' + ansiColorCode + 'm';
      previousColor = ansiColorCode;
    }
    ascii += '#';
  }

  return ascii + '\033[0m';
}

function sortColors(countedColors) {
  var sortedColors = []
    , color;

  for (color in countedColors) {
    sortedColors.push({'color': color, 'count': countedColors[color]});
  }
  return sortedColors.sort(s);

  function s(a, b) {
    if (a.count < b.count) {
      return 1;
    } else if (a.count > b.count) {
      return -1;
    }
    return 0;
  }
}

function countColors(image, asciiWidth, asciiHeight) {
  var colorCount = []
    , blockWidth  = image.width / asciiWidth
    , blockHeight = image.height / asciiHeight
    , index = 0
    , pixelIndex
    , pixelColor;

  for (var i = 0; i < image.pixels.length; i += 3) {
    pixelIndex = i / 3;
    index = Math.floor(pixelIndex / blockWidth) % asciiWidth + 
            Math.floor(pixelIndex / image.width / blockHeight) * asciiWidth;
    if (!colorCount[index]) {
      colorCount[index] = {};
    }
    pixelColor = colors.match(image.pixels[i], image.pixels[i + 1], image.pixels[i + 2]);
    if (!(pixelColor in colorCount[index])) {
      colorCount[index][pixelColor] = 0;
    }
    // Assign less weight to grayscale values for a more interesting picture
    if (pixelColor >= 232 || pixelColor === 59) {
      colorCount[index][pixelColor] += .5;
    } else {
      colorCount[index][pixelColor]++;
    }
  }

  return colorCount;
}

Documentation

JPEG parsing uses libjpeg, and PNG parsing uses pngjs.

The read() function assumes that colors fit in 8-bit integers and have 4 channels for PNGs, and 3 for JPEGs.

Installation

$ npm install pixelr

License

(The MIT License)

Copyright (c) 2013 Bill Yeh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-pixelr's People

Contributors

billyeh avatar darul75 avatar elijahparker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-pixelr's Issues

Compilation issue on Mac

Hello Billyeh,

It is not a real issue, it works fine with png, but with jpeg I deal with problem and my OS environnement.

In runtime I get following error:
"
Wrong JPEG library version: library is 90, caller expects 80.
"

Do you know any tricks to recompile with 90 library instead of 80.

As I can not find any real way to remove 90 version from my computer without breaking something else.

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.