Create PNG image from buffer

3,649 views
Skip to first unread message

msdark

unread,
May 31, 2011, 4:03:34 PM5/31/11
to nodejs
(sorry for my english)..

Hi! .. i have a little app in c++ that capture images and send it to a
nodejs server using thrift [1] ..

The image data is converted to PNG data in the c++ side and sended to
the nodejs using thrift, the data is received like str ..

In the server.js i write the methods to read the data and put into a
Buffer, then using node-png i try to write the image to disc, but i
can't see the image, i only have a dirty image like a TV when the
signal is bad.

This is my nodejs server (with thrift).

var Png = require('png').Png;
var fs = require('fs');
var sys = require('sys');
var thrift = require('thrift');
var Buffer = require('buffer').Buffer;

var ImageService = require('./ImageService.js'),
ttypes = require('./service_types.js');

var indice=0;
var receiveImage = function(image,success){
console.log("Recibida: ");
var rgb = new Buffer(image.data);
var png = new Png(rgb,image.width,image.height,'rgb');
png.encode(function(data,error){
if(error){
console.log('Error: '+error.toString());
process.exit(1);
}else{
fs.writeFileSync('png'+indice
+'.png',data.toString('binary'),'binary');
}
});
success(true);
}

var server = thrift.createServer(ImageService,{
receiveImage: receiveImage
});

server.listen(9090,function(){
console.log("Escuchando...\n");
});


Any idea or guideline???

msdark

unread,
May 31, 2011, 4:06:35 PM5/31/11
to nodejs

Linus G Thiel

unread,
May 31, 2011, 4:24:15 PM5/31/11
to nod...@googlegroups.com
Hi msdark,

I'm not familiar with node-png or thrift but I've done something
similar with node-gd. I fetched the image (with mikeal's request) and
gave it straight to node-gd which works fine. It might be something
with binary encoding in your example? Anyway, here's my code (in
CoffeeScript) if it's of any help!

request
uri: 'https://graph.facebook.com/' + id + '/picture?type=large',
encoding: 'binary',
(error, response, body) ->
# If all went well (FIXME: Otherwise we might want to try
again a few times)
if not error and response.statusCode is 200
try
# Load image directly
image = gd.createFromPngPtr body

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

--
Linus G Thiel
Hansson & Larsson
http://hanssonlarsson.se/
+46 709 89 03 85

Linus G Thiel

unread,
May 31, 2011, 4:27:04 PM5/31/11
to nod...@googlegroups.com
I'm guessing the image object is created in your ImageService? I guess
that's where the data gets mangled.

Onteria

unread,
May 31, 2011, 4:34:43 PM5/31/11
to nodejs
>    var rgb = new Buffer(image.data);

Buffers created from strings are utf8 by default:

> new Buffer(str, encoding='utf8')

Try setting the encoding explicitly to binary like so:

var rgb = new Buffer(image.date, 'binary');
Reply all
Reply to author
Forward
0 new messages