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.

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 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=;
        int w = bi.getWidth();
        int h = bi.getHeight();
        //to create grayscale image
        BufferedImage image = new BufferedImage(w, h,  
        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.
Be a part of the DaniWeb community

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