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

Recommended Answers

All 3 Replies

If you need to make the calculations yourself at the pixel level, you first calculate the luminence from the RGB values y = .30*R + .59*G + .11*B and then set the R,G,and B values to that calculated value.
http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale

Drawing the image to a new BufferedImage of TYPE_BYTE_GRAY, as mentioned in the previous post, is probably the easiest though.

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();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.