node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org ยป

image-grayscale

image-grayscale

Grayscale for images

About

  • takes file path, buffer (inc. vinyl files) or stream buffer (inc. vinyl files)
  • return grayscale version using included or custom algorithms

Installation

$ npm install --save image-grayscale

Important!!1

Works on node >=4.2.1

Supported formats: png, jpg, gif (1 frame from animated ones, no transparency support for now).

Problems with instalation? Most likly lwip :) www.npmjs.com/package/lwip#installation

Examples

Write to file

var imageGrayScale = require('image-grayscale');
var globby = require('globby');

globby(['./src/**/*.*', '!./src/5120x3200.jpg']).then(function (paths) {

  // all@once
  paths.forEach(function(file) {
    imageGrayScale(file, {
      logProgress: true
    });
  });

});

Gulp

With through2:

var gulp = require('gulp');
var through = require('through2');
var imageGrayScale = require('image-grayscale');

gulp.task('default', function () {
  return gulp.src(['./src/**/*.*', '!./src/**/5120x3200.jpg'])
    .pipe((function () {
      return through.obj(function (file, encoding, callback) {

        if (file.isNull() || file.isDirectory()) {
          this.push(file);
          callback();
          return;
        }

        imageGrayScale(
          file,
          {
            bufferMode: true,
            logProgress: true
          },
          function (err, buff) {
            if (err) {
              console.log(err);
            }
            if (buff) {
              file.contents = buff;
            }
            this.push(file);
            callback();
          }.bind(this)
        );

      });
    }()))
    .pipe(gulp.dest('./dist'));
});

Better use gulp-image-grayscale based (from v1.0.0) on this module:

var gulp = require('gulp');
var gulpImageGrayScale = require('gulp-image-grayscale');

gulp.task('default', function () {
  return gulp.src(['./src/**/*.*', '!./src/**/5120x3200.jpg'])
    .pipe(gulpImageGrayScale({
      logProgress: true
    }))
    .pipe(gulp.dest('./dist'));
});

Stream (one file)

var imageGrayScale = require('image-grayscale');
var fs = require('fs');

var readStream = fs.createReadStream('./src/reaper.png');

imageGrayScale(readStream, {
  logProgress: true
});

Return buffer

var imageGrayScale = require('../lib');
var globby = require('globby');

globby(['./src/**/*.*', '!./src/**/5120x3200.jpg']).then(function (paths) {

  // all@once
  paths.forEach(function(e) {
    imageGrayScale(e, {
      logProgress: true,
      bufferMode: true
    }, function(err, buff) {
      console.log(buff);
    });
  });

});

Arguments

file - file path, buffer or stream

options - change some

callback - callback after finish/error

options

var defaults = {

  // default grayscale algorithm, pass own func here
  algorithm: 'lightness',

  // log progress, to 'hide' errors use showErrorMessages
  logProgress: false,

  showErrorMessages: true,

  // dist path for default file mode
  dist: './dist',

  // use default file mode, true returns buffer in callback
  bufferMode: true,

  // if true requires own alhotrithm
  alphaIsReturned: false,

  // to change format...
  // gulp - use additional plugin (gulp-rename)
  changeFormat: {
    png: 'png',
    jpg: 'jpg',
    gif: 'gif'
  },

  // image quality options
  lwip: {
    jpg: {
      quality: 80
    },
    png: {
      compression: 'fast',
      interlaced: false,
      transparency: 'auto'
    },
    gif: {
      colors: 256,
      interlaced: false,
      transparency: 'auto',
      threshold: 50
    }
  },

  // replace logName
  logName: null,

  // custom log func, default is require('gulp-util').log,
  // console.log would do
  log: null

};

algorithm

average -> (r + g + b) / 3;
luminosity -> 0.21 * r + 0.72 * g + 0.07 * b;
lightness (default) -> 0.5 * (max(r, g, b) + min(r, g, b));

own -> algorithm: function(r, g, b, a) {
  return r * 0.25 + g * 0.5 + b * 0.25;
}

ownWithAlpha -> algorithm: function(r, g, b, a) {
  return [r * 0.25 + g * 0.5 + b * 0.25, a/2];
}

own - alpha support requires alphaIsReturned set to true in options

lwip

lwip options.

callback

error - string

buffer - if in bufferMode

options - file options to inspect