Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to access pixels of an image in JavaScript to do Image Processing?

I am trying to perform simple operations on image using javascript. To get the pixels of the image, I am drawing the image on canvas and then get the ImageData from canvas. But for large images, drawing them on the canvas takes a lot of time.

Is there any other way of getting the image pixels without using the canvas element?

like image 973
Pulkit Goyal Avatar asked Jun 13 '11 12:06

Pulkit Goyal


People also ask

Can JavaScript do image processing?

js is a powerful tool which provides a wide range of functions. Other than Filtrr2 and CamanJS, it works with WebGL. The cool thing about this is, that image processing operations are done using the graphic card and can therefore run in real-time. The main drawback is that it will only be supported in newer browsers.

How can I get image data from image?

Definition and Usage. The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas. Note: The ImageData object is not a picture, it specifies a part (rectangle) on the canvas, and holds information of every pixel inside that rectangle.

What is pixel data in image processing?

Each of the pixels that represents an image stored inside a computer has a pixel value which describes how bright that pixel is, and/or what color it should be. In the simplest case of binary images, the pixel value is a 1-bit number indicating either foreground or background.


2 Answers

I don't think you can have image manipulation in JavaScript with hardware acceleration, so no matter if it's canvas or other technology you won't gain much marginal benefit within JavaScript environment.

If you want your code to be hardware accelerated, your code must be compiled into something that is ran in a GPU and has access to graphics memory. Flash and Silverlight's approach is introducing shading language like AGAL and HLSL. Maybe JavaScript will have something like this in the future.

So my suggestion is to do image processing with:

  1. Server side technology
  2. Flash and Silverlight with shading language
like image 147
Cat Chen Avatar answered Sep 23 '22 17:09

Cat Chen


I don't have much experience with Javascript but Max Novakovic(@betamax) wrote a nice a tiny jQuery plugin called $.getImageData which might be helpful.

You can read more about $.getImageData on the disturb blog and the plugin page

close pixelation example

You should be able to access pixels through something like context.getImageData(x, y, width, height).data.

Also, you mentioned hardware acceleration, and that should be available in Firefox4 and Chrome. Shadertoy uses GLSL shaders in the browser:

ShaderToy preview

like image 21
George Profenza Avatar answered Sep 19 '22 17:09

George Profenza