In this post, I will show you the code for extracting gif frame images from an animated gif image.
I will show you two types of code:
- Simply extracting gif images stored in the original animated gif image
- Creating gif image by rendering and accumulating each gif frames stored in the original gif image
Code for Extracting Gif Image From An Animated Gif
Java Code
package com.dukesoftware.utils.image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class GifImageUtils {
public static void main(String[] args) throws IOException {
String sourceGif = "c:/temp/SmallFullColourGIF.gif";
saveAnimatedGifFramePartsToImage(sourceGif, "c:/temp/gif_parts");
}
public static void saveAnimatedGifFramePartsToImage(String input, String outDir) throws IOException
{
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
try(ImageInputStream ciis = ImageIO.createImageInputStream(new File(input)))
{
reader.setInput(ciis, false);
for (int i = 0, noi = reader.getNumImages(true); i < noi; i++) {
BufferedImage image = reader.read(i);
ImageIO.write(image, "GIF", new File(new File(outDir), i + ".gif"));
}
}
}
}
Example Result
I will show you the example execution result of above code.I picked up from the following source gif image from Wikipedia.
"SmallFullColourGIF" by GDallimore - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:SmallFullColourGIF.gif#mediaviewer/File:SmallFullColourGIF.gif
Following Gif images are generated from the above Java source animated "SmallFullColourGIF".
Creating Gif Image by Rendering And Accumulating From Each Animated Gif Frames
Java Code
package com.dukesoftware.utils.image;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class GifImageUtils {
public static void main(String[] args) throws IOException {
String sourceGif = "c:/temp/SmallFullColourGIF.gif";
saveAnimatedGifFrameToImage(sourceGif, "c:/temp/gif_frames");
}
public static void saveAnimatedGifFrameToImage(String input, String outDir) throws IOException
{
final String[] imageatt = new String[]{
"imageLeftPosition",
"imageTopPosition",
};
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
try(ImageInputStream ciis = ImageIO.createImageInputStream(new File(input)))
{
reader.setInput(ciis, false);
BufferedImage master = null;
for (int i = 0, noi = reader.getNumImages(true); i < noi; i++) {
BufferedImage image = reader.read(i);
if(i==0){
master = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
}
IIOMetadata metadata = reader.getImageMetadata(i);
Map<String, Integer> imageAttr = extractMetaData(imageatt, metadata);
Graphics graphics = master.getGraphics();
Integer x = imageAttr.get("imageLeftPosition");
Integer y = imageAttr.get("imageTopPosition");
graphics.drawImage(image, x == null ? 0 : x , y == null ? 0 : y, null);
ImageIO.write(master, "GIF", new File(new File(outDir), i + ".gif"));
}
}
}
private static Map<String, Integer> extractMetaData(String[] imageatt, IIOMetadata metadata) {
Node tree = metadata.getAsTree("javax_imageio_gif_image_1.0");
NodeList children = tree.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node nodeItem = children.item(j);
if(nodeItem.getNodeName().equals("ImageDescriptor")){
return createImageAttrMap(imageatt, nodeItem.getAttributes());
}
}
return Collections.emptyMap();
}
private static Map<String, Integer> createImageAttrMap(String[] imageatt, NamedNodeMap attr) {
Map<String, Integer> imageAttr = new HashMap<>();
for (String att : imageatt) {
Node attnode = attr.getNamedItem(att);
if(attnode != null)
{
imageAttr.put(att, Integer.valueOf(attnode.getNodeValue()));
}
}
return imageAttr;
}
}
Example Result
Following Gif images are generated from the above source animated "SmallFullColourGIF".








































コメント