Tuesday, December 18, 2012
Today I am going to post how you can convert any image to gray scale in Java. First the image is read from a file using javax.imageio.ImageIO class as a BuffertedImage. A function is written that takes in a BufferedImage as parameter and returns a color converted image. An instance of java.awt.image.ColorConvertOp is created which takes in an object of java.awt.color.ColorSpace as first parameter and null as 2nd parameter( since there is no RenderingHints). Then the filter(src,dest) method is called which actually does the work. Then the final image is written to a file in jpeg format as dest.jpg .
How ColorCovertOp works : This class performs a pixel-by-pixel color conversion of the data in the source image. The resulting color values are scaled to the precision of the destination image. Color conversion can be specified via an array of ColorSpace objects or an array of ICC_Profile objects.
ColorSpace class : This is an abstract class whose getInstance(int) method is called to get an instance and CS_GRAY is passed as parameter which defines a built in linear gray scale color space.

Screenshots of images are shown below
Original image
Gray Scale image









--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class GrayScale extends JPanel{
    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        try{  
//reading from file
            BufferedImage src=ImageIO.read(new File("src.jpg"));
            BufferedImage dest=convertToGray(src); 
//getting destination image
            g2d.drawImage(dest,0,0,this); 
/* drawing the image on panel and then writing to a file */

            ImageIO.write(dest,"jpeg",new File("dest.jpg"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    public BufferedImage convertToGray(BufferedImage src){

        //creating instance and using in-built linear gray scale
        ColorConvertOp grayOp = new ColorConvertOp(
            ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
        return grayOp.filter(src,null); 
//converting to gray scale
    }
   
    public static void main (String[] args) {
        JFrame jf=new JFrame("GRAY_SCALE");
        GrayScale obj=new GrayScale();
        jf.getContentPane().add(obj);
        jf.setVisible(true);
        jf.setSize(320,235);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
    }
}

--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Related Posts
--------------------------------------------------------------------------------------------------------------------------
Reflect or flip an image
Change brightness of image using RescaleOp
Java program to draw partially transparent image using Graphics2D

5 comments:

  1. Hi this right information. Thanks for sharing the nice post.


    Convert Image

    ReplyDelete
  2. Thanks a lot , that was a free useful code , do you have any similar post about getting the Cr and Cb and Y of a jpeg image ?

    ReplyDelete
  3. Nice stuff dude! =] Greetings from Brazil.

    ReplyDelete
  4. If you would like to learn more easy scales, you should learn the E minor Pentatonic and the A minor Pentatonic scale. Both of these scales are very commonly used, especially in the blues and Rock 'n' Roll.
    Scales are essential for writing songs and playing guitar solos, so get to it!

    Private guitar instruction is the best way to go but it can be expensive and the quality of the instructors varies greatly. For those who live in small towns there may not be any. If you can't take lessons from me or another good guitar instructor, I suggest that you try one of these guitar systems. Here are my top recommendations for beginners and intermediate level players.


    ReplyDelete
  5. how to convert all images into grayscale in a folder full images sir.

    ReplyDelete

Total Pageviews

Followers


Labels

Popular Posts

free counters