1

I've got an image folder with about 100 sub-folders and in each there are about 5 JPGs.

I'm trying to use node to loop through all the images and resize them so the smallest dimension is 1200. They are a mix of portrait and landscape and the smallest dimension (w or h) should be 1200 scaling the other dimension to maintain the aspect ratio.

I'm learning node so don't know the platform too well but am pretty confident with JS UI-side.

I found an npm walker module, npm gm module, which work for a single JPG so thought and thought it would be pretty easy but I'm having an issue looping through 500+ images then the gm() code is deciding to do it's stuff after the loop has finished but things are crashing out.

Here's my code...

https://runkit.com/nmiddleweek/58d40b478c992a00146502ba

(Shall I inline the code?)

Here's the console error...

This gets thrown after approx 450 iterations...

processing... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-bruchetta-3500px-1.jpg
Finished... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-bruchetta-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-caprese-3500px-1.jpg
processing... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-caprese-3500px-1.jpg
/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:228
    proc.stdin.once('error', cb);
              ^

TypeError: Cannot read property 'once' of undefined
    at gm._spawn (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:228:15)
    at gm._exec (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:190:17)
    at gm.proto.(anonymous function) [as size] (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/getters.js:68:12)
    at resizeFile (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/scripts/imageResizer.js:17:22)
    at Walker.<anonymous> (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/scripts/imageResizer.js:67:21)
    at emitTwo (events.js:106:13)
    at Walker.emit (events.js:191:7)
    at /Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/walker/lib/walker.js:98:12
    at FSReqWrap.oncomplete (fs.js:117:15)
JEG-UPD-PRT0018:scripts nick.middleweek$

Uncommenting line 66...

If I uncomment line 66 and force a reduction in files being processed to 20, I get a different output and the gm module is being invoked with other console.logs. See below...

.
.
.
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Hungarian/istock-537623284.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Hungarian/istock-537729484.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-184348412.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-493159114.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-513809654.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-519681814.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-609908738.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-625144420.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/istock-496311554.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/istock-457422997.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/italian-bruchetta-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/italian-caprese-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/shutterstock_504548443.jpg
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
done
done
done
done
done
done
done
done
done
JEG-UPD-PRT0018:scripts nick.middleweek$

So, here the gm module is being executed and I can check that some of the JPGs on disk have been resized and aspect ratio is maintained - awesome!

What's odd though, is the order in which in the logs are coming out in... Why is Node is executing the code in batches like this?

Is this a synch/ asynch issue? And how do I change it to...

a). Allow me to process hundreds of images b). Process the images to at least solve this problem so it just works properly :-)

Thanks in advance for any help and ideas.

Cheers, Nick

Nick Middleweek
  • 1,049
  • 2
  • 11
  • 19
  • Not sure why you would do this using `node` at all? Are you on macOS, Linux or Windows? I would just use **GNU Parallel** and **GraphicsMagick** or **ImageMagick** at the command line and get them all done in an instant. – Mark Setchell Apr 04 '17 at 10:19
  • Thanks for your reply Mark. I'm using Node for this, because I want to learn Node but I see your point, although if I did this from the command line, I'd need to write a shell script rather than just call a single IM or GM command. I'm on OSX but I want to achieve this in Node. The resizeFile() function in my Node script is working fine, I'm just getting caught out with the execution flow. I'm pretty sure I need to use something like async.parallel. Thanks – Nick Middleweek Apr 04 '17 at 15:47

0 Answers0