0.1.0 • Published 5 years ago
@ismay/ndarray-pixelsort v0.1.0
ndarray-pixelsort
pixelsort images
Based on the algorithm popularised by Kim Asendorf. It sorts the pixels in an image by moving from the upper left to the bottom right pixel in a horizontal fashion, and checking for each pixel whether it matches a filter condition. If it does, it and all the following pixels that match the filter will be sorted according to a sorting algorithm. To sort in a different direction, rotate or flip the image accordingly before sorting.
Installation
$ npm install @ismay/ndarray-pixelsort
Example
So for example:
const util = require('util');
const getPixels = util.promisify(require('get-pixels'));
const savePixels = require('save-pixels');
const fs = require('fs');
const streamToPromise = require('stream-to-promise');
const pixelsort = require('@ismay/ndarray-pixelsort');
/**
* The filter function should accept red, green and blue values (from 0 - 255), and return a boolean
* that is used to decide whether to filter or not
*/
const filter = (red, green, blue) => red > 0 && green > 0 && blue > 0;
/**
* The sort function is used to sort an array of colors, where the colors are arrays of red, green
* and blue values (from 0 - 255). The sorting is done with the native `sort` array method
*/
const sort = (a, b) => (a[0] + a[1] + a[2]) - (b[0] + b[1] + b[2]);
getPixels('./image.png')
.then(image => {
const sorted = pixelsort(image, filter, sort);
return streamToPromise(savePixels(sorted, 'png'));
})
.then(sortedImage => {
fs.writeFileSync('./sorted.png', sortedImage);
});
});
API
pixelsort(image, filter, sort) ⇒ ndarray
Pixelsort images
Kind: global function Returns: ndarray - The pixelsorted ndarray
Param | Type | Description |
---|---|---|
image | ndarray | The input image |
filter | function | The filter function |
sort | function | The sort function |
License
0.1.0
5 years ago