Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By doWhile

Thread: Graysacling an image?

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Graysacling an image?

    I have imported an image(into a BufferedImage), I want to grayscale this image. How can I do it?
    One guy said something like this:
    The way to convert an RGB value to grayscale is to use the following formula: brightness = 0.212671 * R + 0.715160 * G + 0.072169 * B. Assign brightness to the Red, Green, and Blue components of the a new RGB value.
    But I have no clue how to do this. BufferedImage store its RBG in a int. If I could use WriteableRaster and use the formula above, but I dont know how to convert a WriteableRaster to BufferedImage.
    Some help?

  2. #2
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Graysacling an image?

    This work:
    Java Code:
        public static void main(String[] args) throws Exception
        {
    		BufferedImage img = ImageIO.read(new File ("C:\\ngchem.png"));
    		BufferedImage grayimg = new BufferedImage (img.getWidth (), img.getHeight (), BufferedImage.TYPE_BYTE_GRAY);
    
    		for (int i = 0; i < img.getHeight (); i++)
    			for (int j = 0; j < img.getWidth (); j++)
    			{
    				int pixel = img.getRGB (j, i);
    				grayimg.setRGB (j, i, pixel);
    			}
    
    		ImageIO.write(grayimg, "PNG", new File ("C:\\test.png"));
    	}
    But the quality is poor

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Graysacling an image?

    See the following class: ColorConvertOp (Java Platform SE 6)
    It makes convertion of Images to different color spaces much easier, you just need to specify which ColorSpace's for the conversion, and supply the appropriate BufferedImages of a given type...the example below demonstrates an example of how to accomplish this.
    Java Code:
    BufferedImage image = //get color image
    BufferedImage bw = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    BufferedImageOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    bw = op.filter(image, bw);
    I'd recommend immersing yourself in the API of these classes and methods, as there are many different options you can set for the conversion.
    Pojahn_M likes this.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  3. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  4. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •