Grayscale is stored in one value, black, while RBG is held in three, red, blue and green. The best you can do with this is a monochrome image, which you can do with the method getRGB(x, y)in the class BufferedImage. Since your input image is in shades of gray, you can take three color values from them because they must be the same. Then use this value for any color you choose to be the basis of monochrome.
Here is an example with red:
public BufferedImage changeToRedMonochrome(BufferedImage grayImage)
{
int width = grayImage.getWidth();
int height = grayImage.getHeight()
BufferedImage redImage = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB);
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
Color grayColor = new Color(grayImage.getRGB);
int gray = grayColor.getRed();
int red = (gray > 127) ? 255 : gray/2;
int blue = (gray > 127 ? gray/2 : 0;
int green = (gray > 127 ? gray/2 : 0;
Color redColor = new Color(red, blue, green);
redImage.setRGB(x, y, redColor);
}
}
}
This is not a perfect code, and, of course, you will need to customize it according to your specific needs, but this is one way to make a monochrome image.
source
share