Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert GIF image to PNG format

Tags:

java

png

gif

I have to build a Java servlet that receives an image and returns that image converted to PNG format. How can I achieve this? By converting I don't mean changing the file extension, like some examples suggest.

Thanks in advance!

like image 405
farough Avatar asked Nov 17 '10 18:11

farough


People also ask

Can you convert GIF to PNG?

Select a GIF file to convert by uploading it from your computer or a cloud storage service like Google Drive or Dropbox. You can also just drag-and-drop your file into the box to upload. Once you have done this, our free* online tool will begin to convert your GIF file into a PNG image.

How to convert GIF to JPg in Java?

In Java, write() method is used to convert an image from gif type of format to jpg, use the static method write() provided by the class ImageIO under javax. imageio package. Parameters: write() method accepts 3 parameters namely image, formatName and output.


2 Answers

Try this out:

package demo;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class Main {
    public static void main( String [] args ) throws IOException { 
        File input = new File("input.gif");
        File output = new File("output.png");

        ImageIO.write( ImageIO.read( input ), "png", ouput);
    }
}

Read ImageIO.

Of course, you may want to read and write from an stream instead.

like image 126
OscarRyz Avatar answered Sep 20 '22 14:09

OscarRyz


ImageIO.write(ImageIO.read(new File("img.gif")), "png", new File("img.png"));
like image 45
dacwe Avatar answered Sep 19 '22 14:09

dacwe