I am using following code to convert my image into grey scale, on local machine it is fine but when I deploy the same on server it passes Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1 I wonder is it due to different domain, I am not sure but problem is due to "getImageData". Also, this problem is only on chrome.
I have read lot of posts and tried a lot, but seems like nothing working for me.
Thanks in Advance...
function grayscale(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = img;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (var y = 0; y < imgPixels.height; y++) {
for (var x = 0; x < imgPixels.width; x++) {
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
I am using following code to convert my image into grey scale, on local machine it is fine but when I deploy the same on server it passes Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1 I wonder is it due to different domain, I am not sure but problem is due to "getImageData". Also, this problem is only on chrome.
I have read lot of posts and tried a lot, but seems like nothing working for me.
Thanks in Advance...
function grayscale(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = img;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (var y = 0; y < imgPixels.height; y++) {
for (var x = 0; x < imgPixels.width; x++) {
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
Share
Improve this question
asked Nov 7, 2011 at 10:31
RajRaj
401 silver badge5 bronze badges
5
- did you try an image from the same domain?? – philipp Commented Nov 7, 2011 at 15:18
- Hi, Yes, images are in same domains, hosted by same service, moreover, when I gave images locally, and then host it on server the problem is same. – Raj Commented Nov 7, 2011 at 16:46
- developer.mozilla/en/… --- could this be a reason?? – philipp Commented Nov 8, 2011 at 7:56
- . . Duplicate of <stackoverflow./questions/2923564/…> (among others). . . tl;dr: The image is not fully loaded when you try to call "drawImage". – diego nunes Commented Jun 27, 2012 at 17:31
- possible duplicate of Uncaught Error: INDEX_SIZE_ERR – Marcel Gwerder Commented Apr 16, 2014 at 7:39
3 Answers
Reset to default 3This works fine for me
// img is HTMLImageElement
function makeGreyScale(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = img.src;
imgObj.onload = function(){
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
img.src= canvas.toDataURL();
};
}
Most likely you are not calling the script using window.load and rather using on DOM load. I ran into the same problem using the grayscale script. Here's the code that works for me:
$(window).load(function(){
imageColorize();
});
//Black and white images to color
function imageColorize(){
// Fade in images so there isn't a color "pop" document load and then on window load
$(".colorize img").fadeIn(500);
// clone image
$('.colorize img').each(function(){
var el = $(this);
el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
var el = $(this);
el.parent().css({"width":this.width,"height":this.height});
el.dequeue();
});
this.src = grayscale(this.src);
});
// Fade image
$('.colorize img').mouseover(function(){
$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
});
$('.colorize .img_grayscale').mouseout(function(){
$(this).stop().animate({opacity:0}, 1000);
});
}
// Grayscale w canvas method
function grayscale(src){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = src;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
I just had similar issue in IE9, IE10.
I would suggest using, in your case ( creating image object dynamically ):
imgObj.naturalWidth;
imgObj.naturalHeight;
instead of
imgObj.width;
imgObj.height;