Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an image to rows of grayscale pixel values

Tags:

node.js

indico

I'd like to use the node indico API. I need to convert the image to grayscale and then to arrays containing arrays/rows of pixel values. Where do I start?

These tools take a specific format for images, a list of lists, each sub-list containing a 'row' of values corresponding to n pixels in the image.

e.g. [[float, float, float ... *n ], [float, float, float ... *n ], ... *n]

Since pixels tend to be represented by RGBA values, you can use the following formula to convert to grayscale.

Y = (0.2126 * R + 0.7152 * G + 0.0722 * B) * A

We're working on automatically scaling images, but for the moment it's up to you provide a square image

like image 256
Tom Avatar asked Mar 20 '15 21:03

Tom


People also ask

How do I convert an image to gray scale?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

How many pixels is a grayscale image?

For a grayscale or b&w image, we have pixel values ranging from 0 to 255. The smaller numbers closer to zero represent the darker shade while the larger numbers closer to 255 represent the lighter or the white shade.


1 Answers

It looks like node's image manipulation tools are sadly a little lacking, but there is a good solution.

get-pixels allows reading in images either from URL or from local path and will convert it into an ndarray that should work excellently for the API.

The API will accept RGB images in the format that get-pixels produces them, but if you're still interested in converting the images to grayscale, which can be helpful for other applications it's actually a little strange.

In a standard RGB image there's basically a luminence score given to each color, which is how bright the color appears. Based on the luminance, a conversion to grayscale for each pixel happens as follows:

Grayscale = 0.2126*R + 0.7152*G + 0.0722*B

Soon the API will also support the direct use of URLs, stay tuned on that front.

like image 180
Slater Victoroff Avatar answered Nov 14 '22 23:11

Slater Victoroff