Unable to apply the LUT as throwing DirectColorModel required exception

I am trying to set Lut file on the image (.tif) using the below code

     ImagePlus imp1 = IJ.openImage("C:\\Users\\insmohamma\\Desktop\\Python_test\\original images\\images\\test_image.tif");
        LUT lut1 = LutLoader.openLut("C:\\Users\\insmohamma\\Desktop\\Python_test\\original images\\Rainbow_RGB.lut");
        new FileSaver(imp1).saveAsJpeg("C:\\Users\\insmohamma\\Desktop\\Python_test\\original images\\Rainbow\\test_image.tif");

This code is working fine and able to apply LUT on a few images. However, I am getting below exception for few other images (which are actual images on which I wanted to apply it),

Exception in thread “Run$_main” java.lang.IllegalArgumentException: DirectColorModel required
at ij.process.ColorProcessor.setColorModel(ColorProcessor.java:102)
at ij.process.ImageProcessor.setLut(ImageProcessor.java:192)
at LUT_Apply.applyRainbowLUT(LUT_Apply.java:30)
at Test.main(Test.java:12)

Please help me on the same, I am struggling last 7 days.

LUTs cannot be assigned to RGB images. They must first be converted to 8-bit, as in the following JavaScript example. Note that Edit>Auto-import must be enabled to run this example in the Script Editor.

  img = IJ.createImage("Untitled", "rgb ramp", 512, 512, 1);
  lut = LutLoader.openLut(IJ.getDir("luts")+"Phase.lut");
  IJ.run(img, "8-bit", "");
1 Like

@Wayne Thank you so much for your reply and your solution worked perfectly!! Thank you.