1.0.2 • Published 8 years ago

image-grayscale v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

image-grayscale

Grayscale for images

About

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

Installation

$ npm install --save image-grayscale

Important

Works on node >=4.2.1

Supported formats: png, jpg, gif (no transparency and loop support for now).

Examples

Write to file

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

globby(['./src/**/*.*', '!./src/**/5120x3200.jpg']).then(function (paths) {
    return Promise.all(paths.map(function (e) {
      return imageGrayScale(e, { logProgress: 1 });
    }));
  })
  .then(() => {
    // fires when no error (not supported file is error) (Promise.all)
    console.log('finish');
  })
  .catch(err => {
    // fires once even if one error (Promise.all)
    if (err) console.log(err);
  });

Gulp

With through2:

var gulp = require('gulp');
var util = require('gulp-util');
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: 1
        })
          .then(buff => {
            if (buff) file.contents = buff;
            this.push(file);
            callback();
          })
          .catch(err => {
            if (err) util.log('gulpfile:', err);
            this.push(file);
            callback();
          });

      });
    }()))

    .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 example)

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

var readStream = fs.createReadStream('./src/alpha.gif');

imageGrayScale(readStream, {
  logProgress: 1
})
  .then(() => {
    console.log('finish');
  })
  .catch(err => {
    if (err) console.log(err);
  });

Return buffer

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

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

  paths.forEach(function (e) {
    imageGrayScale(e, {
      logProgress: 1,
      bufferMode: 1
    })
      .then(function (buff) {
        if (buff) console.log(buff);
      })
      .catch(function (err) {
        if (err) console.log(err);
      });
  });

});

Arguments

file - file path, buffer or stream

options - change some

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,

  // 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

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago