Piping stdout from one command into stdin for another, and then to a writable stream

With the code in app.js I am able to pipe the output from the raspistill command spawned in camera() in camerautil.js to a writable stream. But instead of the writable stream, I want to pipe the output into the convert command spawned in resize() and then pipe stdout from that command to a writable stream.

What I have tried:

var streamOut = fs.createWriteStream('./image.jpg');
camerautil.camera().pipe(camerautil.resize(streamOut, 1, 1));

It throws the following error:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: Cannot pipe. Not readable.
    at WriteStream.Writable.pipe (_stream_writable.js:162:22)
    at Object.exports.resize (/home/pi/dev/app/camerautil.js:27:14)
    at Object.<anonymous> (/home/pi/dev/app/index3.js:5:37)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

This is my (working) code:

app.js

var camerautil = require('./camerautil'),
    fs = require('fs');

var streamOut = fs.createWriteStream('./image.jpg');
camerautil.camera().pipe(streamOut);

camerautil.js

var spawn = require('child_process').spawn,
    Stream = require('stream');

exports.camera = function() {
    var child = spawn('raspistill', ['-w', 320, '-h', 240, '-n', '-t', 1, '-o', '-']);

    var stream = new Stream();

    child.stderr.on('data', stream.emit.bind(stream, 'error'));
    child.stdout.on('data', stream.emit.bind(stream, 'data'));
    child.stdout.on('end', stream.emit.bind(stream, 'end'));
    child.on('error', stream.emit.bind(stream, 'error'));

    return stream;
}

exports.resize = function(streamIn, width, height) {
    var child = spawn('convert', ['-', '-resize', width + 'x' + height, '-']);

    var stream = new Stream();

    child.stderr.on('data', stream.emit.bind(stream, 'error'));
    child.stdout.on('data', stream.emit.bind(stream, 'data'));
    child.stdout.on('end', stream.emit.bind(stream, 'end'));
    child.on('error', stream.emit.bind(stream, 'error'));

    streamIn.pipe(child.stdin);

    return stream;
}

I should mention that I'm running node v0.12.0 on a Raspberry Pi. The raspistill command is for taking pictures with the camera module. The convert command is part of ImageMagick.

It could be simpler to stream into the gm module (A wrapper for Imagemagick). If that will install on a Raspberry PI that is.

var gm = require('gm')
gm(camerautil.camera())
  .resize('100', '100')
  .stream('jpg')
  .pipe(streamOut)

https://github.com/aheckmann/gm#streams

After some fiddling, I found the solution.

var camerautil = require('./camerautil'),
    fs = require('fs');

var streamOut = fs.createWriteStream('./image.jpg');
camerautil.resize(camerautil.camera(), 1, 1).pipe(streamOut);