Extract frame of gif with graphicsmagick in node.js

谁说胖子不能爱 提交于 2019-11-30 05:25:29

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/35500644/extract-frame-of-gif-with-graphicsmagick-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!