Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract frame of gif with graphicsmagick in node.js

I am trying to convert a gif into a png using graphicsmagick on node.js. In their documentation they have the following code:

// pull out the first frame of an animated gif and save as png
gm('/path/to/animated.gif[0]')
    .write('/path/to/firstframe.png', function(err){
    if (err) print('aaw, shucks')
})

But what if I read the data not from a file, but from a stream or buffer? There I do not have to give a path and therefore cannot append the [0].

What I need is something like this:

gm(streamOrBuffer).extractFrame(0)
    .write('/path/to/firstframe.png', function(err){
    if (err) print('aaw, shucks')
})

There was a similar question here, but the poster ended up drawing the gif on a canvas to extract the first frame on the client side.

I could not find any gm command that looked like it could do what I want. Any ideas?

like image 875
AlexanderF Avatar asked Feb 19 '16 08:02

AlexanderF


1 Answers

According to the source you can use .selectFrame(0).

gm(streamOrBuffer)
    .selectFrame(0)
    .write('/path/to/firstframe.png', function(err) {
        if (err) console.log(err);
    });
like image 96
Ben Fortune Avatar answered Sep 25 '22 20:09

Ben Fortune