Does anyone the code to convert a JPEG colour image into a gray scale image??...

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

/**
 *
 * @author VALIANDRA
 */
public class convertgreyscale {
    public static void main(String args[]) throws IOException
    {   
        //input path
        File e = new File( "C:/aaa.jpg" );
        BufferedImage bi= ImageIO.read(e);
        int w = bi.getWidth();
        int h = bi.getHeight();
        //to create grayscale image
        BufferedImage image = new BufferedImage(w, h,  
        BufferedImage.TYPE_BYTE_GRAY);  
        Graphics g = image.getGraphics();
        g.drawImage(bi, 0, 0, null);
        //create output path in the folder Test
        File f = new File( "C:/Test/image.jpg" );
        try {
            ImageIO.write( image, "PNG", f );
        }
        catch ( IOException x ) {
            // Complain if there was any problem writing
            // the output file.
            x.printStackTrace();
        }
    }
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

This article has been dead for over six months. Start a new discussion instead.