Search code examples
javaimagejavax.imageio

Java: Splitting .GIF image in BufferedImages Gives Malformed Images


So I have a very small piece of code, which takes a .gif image as input, and then it splits this .gif image into an array of BufferedImage. After that, it stores the images in the array on the disk. When I do this, the output images contain heavy white-pixel noise which isn't visible in the input .gif image.

Example of the input gif:

Input gif

Example of malformed output image (the 3rd frame in the gif):

Malformed image

The code I am using to split the gif is as follows:

public static void main(String[] args) throws Exception {
        splitGif(new File("C:\\test.gif"));
}

public static void splitGif(File file) throws IOException {
    ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next();     reader.setInput(ImageIO.createImageInputStream(new FileInputStream(file)), false);
    for(int i = 0; i < reader.getNumImages(true); i++) {
        BufferedImage image = reader.read(i);
        ImageIO.write(image, "PNG", new File(i + ".png"));
    }
}

Can anyone help me out?


Solution

  • You solved it yourself, but for good order: every next frame is the accumulation of all prior frames filling up the transparent pixels in the current frame.

    public static void splitGif(File file) throws IOException {
        ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next();
        reader.setInput(ImageIO.createImageInputStream(new FileInputStream(file)), false);
        BufferedImage outImage = null;
        Graphics2D g = null;    
        for (int i = 0; i < reader.getNumImages(true); i++) {
             BufferedImage image = reader.read(i);
             if (g == null) {
                 BufferedImage outImage = new BufferedImage(image.getWidth(), image.getHeight(),
                         BufferedImage.TYPE_4BYTE_ABGR);
                 g = (Graphics2D) outImage.getGraphics();
             }
             g.drawImage(lastImage, 0, 0, null);
             ImageIO.write(outImage, "PNG", new File(i + ".png"));
         }
         if (g != null) {
             g.dispose();
         }
    }
    
    • getGraphics==createGraphics should be balanced be a dispose as documented.