Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to create a GIF from input .png frames? #325

Answered by jcupitt
GavinJoyce asked this question in Q&A
Discussion options

Is it possible to create a GIF from a series of .png input frames?

This example is the best that I've so far found showing how to manipulate a gif using ruby-vips. That starts with a .gif input though where my example just has a couple of .pngs.

require "vips"
frame1 = Vips::Image.new_from_file "frame1.png"
frame2 = Vips::Image.new_from_file "frame2.png"
frames = [frame1, frame2]
new_image = Vips::Image.arrayjoin(frames, across: 1)
new_image = new_image.mutate do |x|
 x.set_type! GObject::GINT_TYPE, "page-height", frame1.height * 2
end
new_image.magicksave("output.gif", format: "gif")

Perhaps unsurprisingly, this outputs a file that is a single image showing the two input frames stacked upon each other, not an animated GIF. I'm hoping that there's a way to specify things like timing between frames. I know it's possible to do this with libraries like gifsicle, but I'm hoping to do as much as possible with ruby-vips

You must be logged in to vote

Hi again,

You've got it, just take the *2 off. I'd write it as:

#!/usr/bin/ruby
require "vips"
frames = ARGV[1..].map do |filename| 
 Vips::Image.new_from_file filename, access: :sequential 
end
image = Vips::Image.arrayjoin frames, across: 1
image = image.mutate do |x|
 x.set_type! GObject::GINT_TYPE, "page-height", frames[0].height 
 x.set_type! Vips::ARRAY_INT_TYPE, "delay", [500, 1000]
end
image.write_to_file ARGV[0]

Then:

$ vips invert ~/pics/k2.jpg x.jpg
$ ./mkgif.rb x.gif ~/pics/k2.jpg x.jpg 

The delays are in milliseconds. You can write animated webp too, of course. There are some more controls for the GIF writer:

https://www.libvips.org/API/current/VipsForeignSave.h...

Replies: 2 comments

Comment options

Hi again,

You've got it, just take the *2 off. I'd write it as:

#!/usr/bin/ruby
require "vips"
frames = ARGV[1..].map do |filename| 
 Vips::Image.new_from_file filename, access: :sequential 
end
image = Vips::Image.arrayjoin frames, across: 1
image = image.mutate do |x|
 x.set_type! GObject::GINT_TYPE, "page-height", frames[0].height 
 x.set_type! Vips::ARRAY_INT_TYPE, "delay", [500, 1000]
end
image.write_to_file ARGV[0]

Then:

$ vips invert ~/pics/k2.jpg x.jpg
$ ./mkgif.rb x.gif ~/pics/k2.jpg x.jpg 

The delays are in milliseconds. You can write animated webp too, of course. There are some more controls for the GIF writer:

https://www.libvips.org/API/current/VipsForeignSave.html#vips-gifsave

You must be logged in to vote
0 replies
Answer selected by GavinJoyce
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /