Re: Graysacling an image?
This work:
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:frusty:
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.
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.