-
Notifications
You must be signed in to change notification settings - Fork 62
Is it possible to create a GIF from input .png frames? #325
-
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 .png
s.
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
Beta Was this translation helpful? Give feedback.
All reactions
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:
Replies: 2 comments
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
thanks!
Beta Was this translation helpful? Give feedback.