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, “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,

#@ 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("")
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), 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", "Bio-Formats Exporter", options_save)
	options_open = "open=" + ome_filename + " color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT""Bio-Formats Importer", options_open)
	# 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)

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?


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



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


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,

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 import ServiceFactory
from import OMEXMLService

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

# Set IDs and metadata
reader = ImageReader();
writer = new ImageWriter();

# Convert the planes
for series in range(reader.getSeriesCount()):
  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