Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating animated GIF with ImageIO?

Tags:

Has anybody managed to convince ImageIO to write an animated GIF, and in particular setting the correct metadata? My first attempt is roughly as follows (where b1 and b2 are BufferedImages):

ImageTypeSpecifier spec = new ImageTypeSpecifier(b1); ImageWriter wr = ImageIO.getImageWriters(spec, "GIF").next(); wr.setOutput(ImageIO.createImageOutputStream(new File("C:\\Flashing.gif"))); ImageWriteParam param = wr.getDefaultWriteParam(); IIOMetadata meta = wr.getDefaultImageMetadata(spec, param); wr.prepareWriteSequence(meta); wr.writeToSequence(new IIOImage(b1, null, null), param); wr.writeToSequence(new IIOImage(b2, null, null), param); 

This appears to almost work, but:

  • I clearly need to somehow set "proper" metadata to set the time between the images and ideally make them loop (I was naively hoping the default would do something sensible, but hey...)
  • whatever metadata it is writing by default is obviously not quite right: the GIMP gives a couple of error messages when loading the file, although embedding the image in a test web page in Firefox does display the two images in very quick succession, which is tantilisingly close to what I want...

If anyone has got writing animated GIFs from ImageIO to work and can share a snippet of working code or how to fix the above problems, it would be greatly appreciated!

like image 807
Neil Coffey Avatar asked Apr 22 '09 15:04

Neil Coffey


1 Answers

I ran across this question, and decided to try it out; It took a small but non-trivial amount create a usable class (thanks ax) -- so I thought I might share the code around: here is a small class for creating an animated gif image from a number of other images. Archived version

like image 199
Elliot Kroo Avatar answered Sep 27 '22 22:09

Elliot Kroo