javascript - How can I extract the first frame of a GIF and save it as a PNG using GraphicsMagick on NodeJS? - Stack Overflow

Here are the steps in which I want to do this:The user gives the server a link to an animated gifThe s

Here are the steps in which I want to do this:

  1. The user gives the server a link to an animated gif
  2. The server uses "request(url)" to retrieve the file
  3. The server generates a thumbnail of the gif's first frame
  4. The server uploads the thumbnail to Amazon S3

The problem exists in Step 3. I can't figure out a way to extract the gif's first frame. I would prefer to use GraphicsMagick but if there is another way you know of that works, I would be most grateful!

NodeJS is being used.

Here are the steps in which I want to do this:

  1. The user gives the server a link to an animated gif
  2. The server uses "request(url)" to retrieve the file
  3. The server generates a thumbnail of the gif's first frame
  4. The server uploads the thumbnail to Amazon S3

The problem exists in Step 3. I can't figure out a way to extract the gif's first frame. I would prefer to use GraphicsMagick but if there is another way you know of that works, I would be most grateful!

NodeJS is being used.

Share Improve this question asked Jun 19, 2013 at 16:27 aleclarsonaleclarson 19.1k16 gold badges66 silver badges98 bronze badges 3
  • There's a GraphicsMagick package for Node.js: aheckmann.github.io/gm – GJK Commented Jun 19, 2013 at 17:23
  • @GJK Yeah that's what I'm using. Unfortunately when I try to add the [0] to end of the url, it is not grabbing the first frame. It is instead just saving the gif in a crappy quality. – aleclarson Commented Jun 20, 2013 at 1:06
  • what graphicsmagick mand do you need to use and what mand is gm generating for you? – Jonathan Ong Commented Jun 20, 2013 at 5:49
Add a ment  | 

2 Answers 2

Reset to default 7

I don't know why this isn't in the documentation, but someone else answered this here on stackoverflow.

So, you can use this either with a file path string or a stream/buffer, like so:

For a file:

gm('/path/to/animated.gif')
.selectFrame(0)
.write('/path/to/firstframe.png', function(err){
    if (err) print('  :(  ');
})

For a stream/buffer:

gm(streamOrBuffer)
.selectFrame(0)
.write('/path/to/firstframe.png', function(err){
    if (err) print('  :(  ');
})

In the documentation they say you need to change the path string to be something like '/path/to/animated.gif[0]' to be more specific with the frame you want to select, but I tested that code without specifying the frame, and it worked fine (using [email protected] and ImageMagick 6.7.7-10 2014-03-06). Also, what they had in the documentation (without using selectFrame) didn't work. It ended up crashing and creating several png frames of the whole gif in the process.

Why they haven't documented selectFrame yet is beyond me. As they pointed out in the cited link, there's an open issue wondering precisely that.

Since I couldn't get it working on the server-side, I ended up just drawing the .gif to a <canvas> element to "simulate" extracting a .gif's first frame. This gave me the paused gif that I was going for.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101645a4611309.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信