Change and save channel LUT with Bio-Formats

Dear @OMETeam,

I would like to change the lookup table in a given image permanently, i.e.I when saving and re-opening the image I would like the LUT change to persist.

Using IJ.run(imp, “Grays”, “”) works using ImageJ saveAs/openImage, but not using Bio-formats, see code below.

How can I achieve this using Bio-formats?
Thanks for your help and cheers,
Kai

#@ File(label="save destination", style="directory", description="") target
#@ Boolean(label="use Bio-Formats?", description="", value="True") use_bf

from ij import IJ
from ij import ImagePlus

imp = IJ.openImage("http://imagej.nih.gov/ij/images/FluorescentCells.zip")
imp.show()
image_dimensions = imp.getDimensions() # (width, height, nChannels, nSlices, nFrames)

luts = ["Grays", "Green", "Magenta"]
for channel in range(image_dimensions[2]):
	print channel + 1
	imp.setC(channel + 1)
	IJ.run(imp, luts[channel], "")

if use_bf == True:
	# save/open with bioformats, color default: all channels LUTS are grey
	ome_filename = str(target) + "/" + "FluorescentCells_LUTSchanged.ome.tiff"
	options_save = "save=" + ome_filename + " export compression=LZW"
	IJ.run(imp, "Bio-Formats Exporter", options_save)
	options_open = "open=" + ome_filename + " color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT"
	IJ.run("Bio-Formats Importer", options_open)
	
else:
	# save/open as IJ-tiff: channel LUTs stay changed
	ij_filename = str(target) + "/" + "FluorescentCells_LUTSchanged.tif"
	IJ.saveAs(imp, "Tiff", ij_filename)
	imp2 = IJ.openImage(ij_filename)
	imp2.show()

Hi Kay

I tested the code you posted and I have the expected result i.e. grey, green, magenta when using B-F.
I am using B-F 6.3.1.
Which version are you testing with?

cheers
Jmarie

Hi @j.burel,

Thanks for testing!
I have repeated the test now on Linux mint with a vanilla Fiji from today using BF 6.3.1 and also just now with a freshly updated BF 6.4.0. I have also tested on Windows 10 with BF 6.4.0, but in all cases the result is grey - grey - grey in the “FluorescentCells_LUTSchanged.ome.tiff”

Hi Kay

When I open the file in preview on mac, I can see the Grey, Green, Magenta. That’s how I checked previously
If I import the image in OMERO, the original image is displayed i.e. no change in color.
and no success when I open the image in ImageJ.
So it looks like a bug. I will create an issue

Thanks

Jmarie

Hi Jmarie,
thanks reproducing the bug for opening the issue!

cheers,
Kai

Hi Jmarie,

Until the bug is fixed, is there maybe an alternative to solve the problem? Since the desired LUT change is part of a IJ-jython script for processing, is there maybe a way to edit the ome-xml metadata in an IJ-jython script?

Thank you and cheers,
Kai

I don’t think there is a simple workaround using the built in exporter from the plugin. The problem seems to be that the exporter isn’t applying the appropriate colour model to the writer. This may be possible to work around by creating a script using the underlying Bio-Formats API but it will be a little more complicated.

I haven’t had a chance to test this but a potential work around may look something like the below code which creates a reader and writer for the conversion and then directly applies a color model to the writer.

from loci.formats import ImageReader
from loci.formats import ImageWriter
from loci.formats import MetadataTools
from loci.common.services import ServiceFactory
from loci.formats.services import OMEXMLService

factory = ServiceFactory();
service = factory.getInstance(OMEXMLService.class);
omeMeta = MetadataTools.createOMEXMLMetadata()

# Set IDs and metadata
reader = ImageReader();
reader.setMetadataStore(omeMeta);
reader.setId("http://imagej.nih.gov/ij/images/FluorescentCells.zip");
writer = new ImageWriter();
writer.setMetadataRetrieve(service.asRetrieve(reader.getMetadataStore()));
writer.setId("/path/to/output/file.ome.tiff");

# Convert the planes
for series in range(reader.getSeriesCount()):
  reader.setSeries(series);
  writer.setSeries(series);
  for image in range(reader.getImageCount()):
    writer.saveBytes(image, reader.openBytes(image));

# Create an IndexColorModel (or Index16ColorModel) for example
channel = zeros(256, 'b')
for i in range(256):
   channel[i] = (i - 128);
colorModel = LUT(channel, channel, channel);

# Apply ColorModel to writer
writer.setColorModel(colorModel)

reader.close();
writer.close();