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
--------------------------------------------------------------------------------------------------------------------------
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 the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Reflect or flip an image
Change brightness of image using RescaleOp
Java program to draw partially transparent image using Graphics2D
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
Labels:Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Hi this right information. Thanks for sharing the nice post.
ReplyDeleteConvert Image
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 ?
ReplyDeleteNice stuff dude! =] Greetings from Brazil.
ReplyDeleteIf 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.
ReplyDeleteScales 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.
how to convert all images into grayscale in a folder full images sir.
ReplyDelete