4

I try to save the tiff instead of coloure gray-scaled. How could I do this? (JAI must be used, because it is a tiff!)

Thanks a lot in advance & Best Regards.

Tim
  • 13,228
  • 36
  • 108
  • 159

2 Answers2

3

What you want is to download the JAI Image I/O Tools, which provides ImageIO adapters for JAI. Once you've installed that, it's smooth sailing.

final BufferedImage in = ImageIO.read(new File("frabozzle.tif"));
final BufferedImage out = new BufferedImage(
    in.getWidth(), in.getHeight(),
    BufferedImage.TYPE_BYTE_GRAY);
out.getGraphics().drawImage(in, 0, 0, null);
ImageIO.write(out, "TIFF", new File("graybozzle.tif"));
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • hmm, and how could I save the image without any compression grayscaled? – Tim Feb 05 '10 at 21:31
  • I get a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6422430. It is in line out.getGraphics(). – Tim Feb 05 '10 at 22:15
  • in = (java.awt.image.BufferedImage) BufferedImage@381d92; type = 0 ColorModel: #pixelBits = 32 numComponents = 1 color space = java.awt.color.ICC_ColorSpace@1d8e92 transparency = 1 alpha = false isAlphaPre = false sun.awt.image.SunWritableRaster@7e5130 – Tim Feb 05 '10 at 22:30
  • Problem is solved. My image, but IrfanView displays it in red color. I do not know why, but I display it in other programs well, but not in GIMP :-( – Tim Feb 05 '10 at 22:50
3

Given a BufferedImage, you can use the filter() method of ColorConvertOp, as shown in this example.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045