Skip to content

Instantly share code, notes, and snippets.

@johntitus
Created November 5, 2012 18:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johntitus/4019396 to your computer and use it in GitHub Desktop.
Save johntitus/4019396 to your computer and use it in GitHub Desktop.
(Node.js) Watermark an image using ImageMagick from two streams.
/**
* Dependencies
*/
var fs = require('fs'),
request = require('request'),
cp = require('child_process'),
spawn = cp.spawn;
var image = request("http://medpreps.com/wp-content/uploads/2012/05/cma-practice-test.jpg");
var wm = request("https://github.com/linse/Gibberbot/diff_blob/f096f6d94b3fb745975ea5f61f5a43b47098598b/res/drawable/droid_watermark.png?raw=true");
// Composite is one of the file that make up image magick.
// The second argument, `+200+200` represent the location on the image where to put the watermark (x,y from top left).
var args = ['-geometry', '+200+200', 'wmPipe', 'basePipe', '-'];
var composite = spawn('composite', args);
/*
* basePipe and wmPipe are two name unix pipes. You can create one with `mkfifo basePipe && chmod 777 basePipe`
*/
image.pipe(fs.createWriteStream("./basePipe"));
wm.pipe(fs.createWriteStream("./wmPipe"));
// Pipe the image magick output to a file. Could just as easily pipe it to a http response or any other stream.
composite.stdout.pipe( fs.createWriteStream("out.jpg") );
@pociej
Copy link

pociej commented Aug 1, 2014

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment