from ij import IJ, ImagePlus, ImageStack from ij.io import Opener from java.awt import FileDialog as FD from java.io import File import re class Stack(ImageStack): def __init__(self, title, imp): ImageStack.__init__(self, imp.width, imp.height) self.title = title self.addSlice(imp.title, imp.getProcessor()) def __add__(self, imp): if imp.width == self.width and imp.height == self.height: self.addSlice(imp.title, imp.getProcessor()) else: print "image", imp.getTitle(), "has different dimensions. Ignoring." return self def show(self): ImagePlus(self.title, self).show() def openAsStack(): fd = FD(IJ.getInstance(), "Select a file in a sequence", FD.LOAD) fd.show() dir, first = fd.getDirectory(), fd.getFile() if None == first: return o = Opener() stack = None ext = first[first.rfind("."):] prefix = first[:re.search('[0-9]+' + ext, first).start()] # find out the base name of the file regexp = re.compile(prefix + '[0-9]+' + ext) # prepare a matching for similar files for name in File(dir).list(): if not regexp.search(name): continue # ignore files whose name doesn't match with the selected. img = o.openImage(dir, name) if None != img: if None == stack: stack = Stack(prefix + " sequence", img) else: stack += img if None != stack: stack.show() openAsStack()