Bioformat does not open files using Jython

Hi everyone,

I struggle hard with the bioformat in jython. I use this script :

from ij import IJ
from ij.process import ImageConverter as IC
import os
import sys
from ij.gui import WaitForUserDialog
from ij.io import FileSaver
from ij.io import DirectoryChooser
from ij import IJ, ImagePlus, VirtualStack
from ij.gui import Overlay
from loci.plugins import BF
 
indc = DirectoryChooser("Input")
outdc = DirectoryChooser("Output")
indir = indc.getDirectory()
outdir = outdc.getDirectory()

if indir==outdir :
	print "Input can't be the same directory as output."


#if (selectionType() != 0) :
#	myWait = WaitForUserDialog ("Warning", "Please, select a proper ROI")
#	myWait.show()

for filename in os.listdir(indir):
	if filename.endswith(".lif"):
		imps = BF.openImagePlus(indir+filename)
		for imp in imps:
			imp.show()
	if filename.endswith(".tif"):
		print "Processing ",filename
		imp = IJ.openImage(os.path.join(indir, filename))
		if imp is None :
			print "Could not open files in the selected directory :", filename
		imp.show() 
	imp=IJ.getImage()
	myWait = WaitForUserDialog ("Waiting", "Select ROI")
	myWait.show()
#		if contains(Roi) == False :
#			print "No ROI selected"
	IJ.run(imp, "Make Inverse", "slice")
	IJ.run(imp, "Clear", "stack")
	IJ.run(imp, "Select None", "")
	filepath=outdir+"/"+imp.title+"_cleared.tiff"
	fs = FileSaver(imp)
	fs.saveAsTiff(filepath)
	imp.close()


I struggle opening .lif files. In a reaaally weird way this codes properly works if I previously open a picture. But with nothing opened, it stops with “There is no image opened” message. It makes this error following the imp = IJ.getImage() command…

I tried for a week between my experiments to resolve this issue… So if you have some suggestions other than linking to website or subject I probably already watched. (Fiji website, a lot of posts concerning bioformat, bioformat library etc.)

Thanks a lot & have a very nice week-end.
Virgile

What if you run only the part before IJ.getImage() (in the editor, Run>Run selected code)
Does any image shows up ?

But I wonder why you use IJ.getImage if you already have a variable imps or imp that contains the ImagePlus object…

1 Like

Really nice suggestion, it opens every picture in the selected directory.

I used IJ.getImage because I am a beginner, and i did not know that I could use it without defining it thanks to the previous imp variable.

I then tried to erase imp = IJ.getImage()
1st : It asks me to select an ROI (waitforuser) , while there are no opened picture.
2nd : It makes an error saying “Imp is not defined”

Update : For a strange reason that makes no sense to me. It now works using the code above. I changed nothing but it now works. Bioformat is a strange world.

1 Like