I have an image, call it grayscale.jpg. Now I open that image in The Gimp and change the color mode to RGB and save it as color.jpg. If I view grayscale.jpg and color.jpg in any image viewer, they look exactly the same. But if I open the images with javax.imageio.ImageIO
import javax.imageio.ImageIO;
input = ImageIO.read(new File("grayscale.jpg"));
System.out.format("Grayscale value: %x\n", input.getRGB(200, 200));
input = ImageIO.read(new File("color.jpg"));
System.out.format("Color value: %x\n", input.getRGB(200, 200));
The color image will return the correct value, say 0xff6c6c6c. The grayscale image will return a different, lighter, incorrect value like 0xffaeaeae.
Grayscale value: 0xffaeaeae // Incorrect (Lighter)
Color value: 0xff6c6c6c // Correct
In other words, javax.imageio.ImageIO thinks grayscale images are much lighter than they actually are. How can I accurately read grayscale images?
Edit
Here's more context. My users upload images, which may be grayscale. My Java runs on the server and does some pretty complex image processing. So an ideal solution is to just fix my Java code as opposed to cobbling something together with command line tools.