5

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?

Community
  • 1
  • 1
AlexanderF
  • 254
  • 3
  • 11

1 Answers1

6

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);
    });
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
  • Thanks, that seems to be it! They even have an issue to add it to the docs [here](https://github.com/aheckmann/gm/issues/461). – AlexanderF Feb 19 '16 at 09:06
  • 1
    Thanks for inspiration, it helped me answering similar question about PDF first page: https://stackoverflow.com/a/51276310/4536543 – Maxim Mazurok Jul 11 '18 at 02:19