tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Image IO > How to create Black and White image from color image

How to create Black and White image from color image 

Java Image IO provides pluggable architecture for accessing images transparently. It is simple, flexible and allows us to plugin various image readers and writers with ease. The following example shows converting colored image to black and white image.

stpatricks_08.gif

File Name  :  
com/bethecoder/tutorials/imageio/BlackAndWhiteTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.imageio;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class BlackAndWhiteTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {

    File file = new File("C:/Temp/stpatricks_08.gif");
    BufferedImage orginalImage = ImageIO.read(file);

    BufferedImage blackAndWhiteImg = new BufferedImage(
        orginalImage.getWidth(), orginalImage.getHeight(),
        BufferedImage.TYPE_BYTE_BINARY);
    
    Graphics2D graphics = blackAndWhiteImg.createGraphics();
    graphics.drawImage(orginalImage, 00null);

    ImageIO.write(blackAndWhiteImg, "png"new File("c:/Temp/stpatricks_08_bw.png"))
  }

}
   

It gives the following output,
stpatricks_08_bw.png


                    Print

   



Books Recommendation

 
 
bl  br