Extract frame of gif with graphicsmagick in node.js

2024/2/27 8:51:57

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?

Answer

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);});
http://en.ppmy.cn/q/41230.html

Related Q&A

Regex for number between 0 to 100?

It should not allow leading zeroes, like 099. Allowed values should be like these: 35, 99, 1, 2, 3, 100.This is What I tried: $(#createCoupon_discountAmount).bind(input propertychange, function () {$(t…

Set text in textarea, including newlines

I want to set the text in a <textarea> from a js-function; Im simply setting the innerText-attribute to a new value. The text is multiline and should be displayed as such. However, newlines are d…

CSS column-count elements jumping across columns

Im trying to get a dynamic amount of elements to show across 5 elements using CSS3 column-count, but when I expand the list item heights on hover, it occasionally causes jumping (an element going to th…

I have no idea Object(this) means

In https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/fillthere is a line like// Steps 1-2. if (this == null) {throw new TypeError(this is null or not defined); }var O …

How to combine two parts of single form in angular?

I am making angular application with angular dynamic form where i am loading data for dynamic form through JSON..JSON has two parts such as part 1 and part 2,jsonDataPart1: any = [{"elementType&qu…

jquery hide a div that contains flash without resetting it

Greetings, I have written a modal using jquery UI and it appears at the front of a flash movie thus the html inside the modal becomes corrupt, I tried to hide the movie right before modal gets triggere…

jstree jquery plugin - Get all child and sub child nodes of parent

I am using the jsTree jquery plugin I am trying to fetch all the nodes and sub nodes of a selected parent. But somehow its not working unless I use recursion. Is there a better way inherent to jsTre…

Understanding Cyclic Redundancy Code algorithm for beginners

at section 5.5 of the PNG Specification, it discusses this concept in the PNG file format called "CRC" or "Cyclic Redundancy Code". Ive never heard of it before, so Im trying to und…

Why `+0n` throws an error but `-0n` does not?

The following code throws an error in javascript:console.log(String(+0n))But this code runs successfully:console.log(String(-0n))Why +0n throws an error but -0n does not?

Why does Date#getHours() return hour + 1?

This is my code:var feedDataTimestamp = new Date("2014-01-14T00:04:40+0000").getTime(); var parsedDate = new Date(+feedDataTimestamp); alert(parsedDate.getHours());but it should print 0, not …