Skip to content

Instantly share code, notes, and snippets.

@romainGuiet
Last active November 15, 2024 15:56
Show Gist options
  • Save romainGuiet/435806a12e548ec1adcc2af1eff1002a to your computer and use it in GitHub Desktop.
Save romainGuiet/435806a12e548ec1adcc2af1eff1002a to your computer and use it in GitHub Desktop.
A groovy script usable in FIJI to export images of a lif file (should work with other bioformats supported format). User can specify the series to export using comma (1,2,3) or a range using dash (1-5) , or nothing to export all series #BIOP #FIJI #groovy #export #bioformats
#@ File image_path
#@ String seriesNbr
#@ String (choices={"max","avg","sum","none"}) zProj
import org.apache.commons.io.FilenameUtils
import ij.*
import ij.plugin.ZProjector
import ij.io.FileSaver
import loci.formats.ImageReader
import loci.plugins.BF
import loci.plugins.in.ImporterOptions
/*
* Get image info : dir, name and create the output_dir
*/
dir = image_path.getParent()
image_name = image_path.getName()
image_basename = FilenameUtils.getBaseName( image_name )
println dir + image_name
// From the user choice, we create a serie , or range of serie
def seriesIdxes
if (seriesNbr.isInteger()) {
seriesIdxes = ( seriesNbr.toInteger() )
} else {
if ( seriesNbr =~ /.*,.*/){
seriesIdxes = ( seriesNbr.split(",") )
} else if ( seriesNbr =~ /.*-.*/){
range = seriesNbr.split("-")
seriesIdxes = ( (range[0] as int) .. (range[1] as int) )
} else if (seriesNbr.equals("")) {
img_rd = new ImageReader()
img_rd.setId( image_path.toString() )
seriesCount = img_rd.getSeriesCount()
seriesIdxes = ( 0 .. seriesCount-1 )
} else {
println "Please enter a valid Serie index(es) :\n- single number \n- a list on number comma separated (1,3,9) \n- a range eg (2-8)"
return
}
}
// create an output dir and prepare output file path
def output_dir = new File( dir , "output" )
output_dir.mkdirs()
imp_option = new ImporterOptions()
imp_option.setId(image_path.toString())
seriesIdxes.each{ serieIdx ->
imp_option.setSeriesOn(serieIdx, true)
// returns a ImagePlus[]
bfimp = BF.openImagePlus(imp_option)
// do a projection
def final_imp = bfimp.first()
image_lif_name = final_imp.getTitle()
image_lif_name = image_lif_name.replaceAll("/","_") // to handle sub folder within lif ile
if ( ! zProj.equals("none") ) final_imp = ZProjector.run(bfimp[0], zProj);
def output_path = new File ( output_dir , image_lif_name+"_"+zProj+"zProj_i" + serieIdx +".tif" )
// save file
def fs = new FileSaver(final_imp)
fs.saveAsTiff(output_path.toString() )
println "From : "+ image_basename + " : " + serieIdx +" serie done!"
imp_option.setSeriesOn(serieIdx, false)
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment