Reputation: 7
I can convert an image to grayscale as I wish, but what I was wondering is about how to go back to original image after converting to grayscale?
Here is my javascript filter image to grayscale
Filters = {};
var img = document.getElementById("background");
var c = document.getElementById('canvas');
var ctx = c.getContext("2d");
Filters.getPixels = function() {
return ctx.getImageData(0, 0, c.width, c.height);
};
Filters.filterImage = function(filter, var_args) {
var args = [this.getPixels()];
args.push(var_args);
return filter.apply(null, args);
};
function runFilter(filter, arg1, arg2, arg3) {
var idata = Filters.filterImage(filter, arg1, arg2, arg3);
c.width = idata.width;
c.height = idata.height;
var ctx = c.getContext('2d');
ctx.putImageData(idata, 0, 0);
}
Filters.grayscale = function(pixels, args) {
var d = pixels.data;
for (var i = 0; i < d.length; i += 4) {
var r = d[i];
var g = d[i + 1];
var b = d[i + 2];
var v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
d[i] = d[i + 1] = d[i + 2] = v;
}
return pixels;
};
Upvotes: 0
Views: 606
Reputation: 1986
Going to grayscale involves throwing away the color data. The only way to get that back is to have saved a copy of it elsewhere.
Upvotes: 3