Open In App

Java Program to Convert GIF Images to JPEG

Last Updated : 25 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

JPEG and GIF both are a type of image format to store images. JPEG uses lossy compression algorithm and the image may lose some of its data whereas GIF uses a lossless compression algorithm and no image data loss is present in GIF format. GIF images support animation and transparency. Sometimes it is required to attach the Image where we required an image file with the specified extension. And we have the image with the different extension which needs to be converted with a specified extension like in this we will convert the image having Extension of .jpeg to .gif and vice-versa.

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.  

Syntax:

boolean write(RenderedImage image, String formatName, OutputStream output)

Parameters: write() method accepts 3 parameters namely image, formatName and output.

  1. Image: The input image as a subclass of the RenderedImage interface, such as BufferedImage.To obtain a BufferedImage object from the input image file, we can use the read(InputStream) which is also provided by the ImageIO class.
  2. formatName: specifies format type of the output image.
  3. output specifies an OutputStream to which the output image will be written.

Return Type: Boolean value where returning true if it can find an ImageWriter and perform conversion successfully else it will return false.

Exceptions: It will throw IOException if an error occurs during execution. It is shown later on in the implementation part.

Following utility class implements a static method, convertImg() which takes input and output image path as parameters.

Procedure:

To convert .jpeg to .gif, in the below code do the following necessary changes

  • Change ‘formatType’ to GIF.
  • In ‘outputImage’ change the name of the image with the correct file extension.
  • Similarly, change name of ‘inputImage’ with the correct file extension.

Implementation:

Note: For input and output images path, Input and input images been passed are present on the desktop of the machine

  • Input image with name “demoImage.gif”
  • Output Image with name “demoImage.jpeg”

Code is present at the directory mentioned below

  • /Users/mayanksolanki/Desktop/Job/Coding/GFG/Folder/

Example

Java




// Java Program to Convert GIF Image to JPEG Image
 
// Importing BufferdImage class from java.awt package
// to describe an image with accessible buffer of image
import java.awt.image.BufferedImage;
// Importing all input output classes
import java.io.*;
// Importing an interface
// to determine the setting of IIOParam object
import javax.imageio.ImageIO;
 
// Class 1
// Helper class
class Helper {
 
    // Method to convert image to JPEG
    public static boolean convertImg(String inputImgPath,
                                     String outputImgPath,
                                     String formatType)
 
        throws IOException
    {
 
        // Creating an object  of FileInputStream to read
        FileInputStream inputStream
            = new FileInputStream(inputImgPath);
 
        // Creating an object  of FileOutputStream to write
        FileOutputStream outputStream
            = new FileOutputStream(outputImgPath);
 
        // Reading the  input image from file
        BufferedImage inputImage
            = ImageIO.read(inputStream);
 
        // Writing to the output image in specified format
        boolean result = ImageIO.write(
            inputImage, formatType, outputStream);
 
        // Closing the streams in order to avoid read write
        // operations
        outputStream.close();
        inputStream.close();
 
        return result;
    }
}
 
// Class 2
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws IllegalStateException
    {
 
        // Here, the local directories from machine
        //  is passed as in strings
 
        // Creating a string to store the path of image
        // to be converted
        String inputImage = "/Users/mayanksolanki/Desktop/demoImage.gif";
 
        // Creating a string to
        // store path of converted image
        String outputImage = "/Users/mayanksolanki/Desktop/demoImage.jpeg";
        // Creating another string that will be
        // store format of converted image
 
        // Simply creating  creating just to hold the format
        // type
        String formatType = "JPEG";
 
        // Try block to check for exceptions
        try {
 
            // result will store boolean value whether image
            // is converted successfully or not
 
            boolean result = Helper.convertImg(
                inputImage, outputImage, formatType);
 
            if (result) {
 
                // Display message when image is converted
                // successfully
                System.out.println(
                    "Image converted to jpeg successfully.");
            }
 
            else {
 
                // Display message when image is not
                // converted successfully
                System.out.println(
                    "Could not convert image.");
            }
        }
 
        // Catch block to handle the exceptions
        catch (IOException ex) {
 
            // Display message when exception is thrown
            System.out.println(
                "Error during converting image.");
 
            // Print the line number
            // where the exception occurred
            ex.printStackTrace();
        }
    }
}


Output:

Case 1: When an error is thrown

 Case 2: Successful compilation but exception thrown at runtime(failed to run properly) 

Case 3: Successful compilation and successfully run 

Image converted to jpeg successfully. 

 When executed it will show Image converted to jpeg successfully, we can find that on console and a new jpeg image created in the file



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads