AlexanderF
AlexanderF

Reputation: 254

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?

Upvotes: 5

Views: 3644

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32137

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);
    });

Upvotes: 6

Related Questions