8

I'm running NodeJS on the server-side, and I'm trying to do a bit of automated image processing to determine the 'base colour' of an image.

Here are the steps of what I want to do:

  1. Take an image (on a remote server, so passed through with a URL) and get its dimensions
  2. Use dimensions to calculate the centre of the image
  3. Take a 10px x 50px (WxL) rectangle around the centre-point
  4. Get the RGB value for each of these pixels (500 per image)
  5. Output the average value of the pixels

I know such things are possible in PHP, but I'd like to use Node is possible. I've seen tutorials on using Node-imagick for basic processing (like resizing and cropping) but don't know where to start with more advanced analysis like this.

Questions

(a) Is this possible with Node?

(b) What libraries will allow me to do this?

JVG
  • 20,198
  • 47
  • 132
  • 210
  • 4
    You might want to check out this answer: http://stackoverflow.com/a/10717592/480807 -- NOTE: I would highly advise offloading this type of action to a background worker of any type as Node's strength lies in asynchronously handling numerous IO blocking operations & NOT CPU blocking/intensive operations (such as image manipulation). That being said GM does do this work with worker threads (not blocking the event loop) – Casey Flynn Jul 12 '13 at 02:27
  • Interesting. can you explain what you mean by `background worker`? I suppose I can use PHP for this without too much hassle, I've just been building everything else in Node so I wanted to keep it all in the same place. But I'd like to do whatever is fastest/lightest load on my server for sure! – JVG Jul 12 '13 at 02:34
  • 4
    Typically in a web application you have your "application server(s)" that are intended to quickly handle the bulk of user requests. For very CPU intensive or time-consuming operations, such as long API requests, image manipulation/processing, calculating a fibonacci sequence (joke), a job is entered into a 'queue' of jobs, which are then picked up one by one with a background worker and completed without burdening the application servers. Check out "gearman" -- it has served me well. – Casey Flynn Jul 12 '13 at 02:39
  • Oh, yep this is what I was doing already actually. The main application (frontend) is built in Angular on top of Node/MongoDB, there is also a scraper which runs once daily to get data and add to the database, I wanted this to run in a similar way, after the scraper finishes, load data from the DB and analyse the images. Are you suggesting I use PHP for this? – JVG Jul 12 '13 at 02:43
  • 1
    For image manipulation I can't really RECOMMEND anything per se, without a lot more analysis. Just make sure you're not doing the image manipulation on the same boxes/servers that you're using to serve your main application. Image manipulation is extremely resource intensive and can slow down your app dramatically. A background worker is typically a different machine pulling jobs from a queue. It sounds like you've put a cron job on your application server and you're doing all of this there. – Casey Flynn Jul 12 '13 at 02:48
  • Very interesting, thanks for explaining - that's all stuff I hadn't considered at all! I suppose what I'll do is build it in PHP on a different server and ship the job off to it. The PHP would have to connect remotely to my DB to update it but I'm sure I can work that out. Cheers mate! – JVG Jul 12 '13 at 02:59
  • 1
    Connecting remotely shouldn't be a problem. There are a lot of job queueing libraries and systems out there, take a look at a couple. Gearman is great because it works for a whole bunch of languages. – Casey Flynn Jul 12 '13 at 03:00

2 Answers2

27

A: yes

B: gm

here are some more characters to make this long enough for stackoverflow...

Community
  • 1
  • 1
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Low-level image processing would most likely not be written in javascript. Systems programming languages are better suited to this. Not that it's not possible, you can read bytes and manipulate them, so it's possible, but it would not be the best fit. – Peter Lyons Jan 30 '20 at 16:01
  • Node is single threaded and image manipulation is usually CPU-bound and highly parallelizable so multithreaded implementations are likely to be faster. – Peter Lyons Feb 04 '20 at 01:50
0

node-itk may be helpful to you.

Node-ITK is a node.js wrapper, which is built on top of ITK. It was built to facilitate noe.js' use in rapid prototyping, education, and web services for Medical Image Processing.

https://npmjs.org/package/node-itk

Jeroen
  • 15,257
  • 12
  • 59
  • 102