def select_images(): """ Returns two ImagePlus objects that can be used by the drift correction. If more than two images are available a dialog is used for selection. """ if WindowManager.getImageCount() > 0: imp = WindowManager.getCurrentImage() if imp.getImageStackSize() == 2: dup = Duplicator() img1 = dup.run(imp, 1, 1) img1.setTitle("Slice1") img2 = dup.run(imp, 2, 2) img2.setTitle("Slice2") elif WindowManager.getImageCount() == 2: img1, img2 = [WindowManager.getImage(id) for id in WindowManager.getIDList()] elif WindowManager.getImageCount() > 2: image_ids = WindowManager.getIDList() image_titles = [WindowManager.getImage(id).getTitle() for id in image_ids] try: sel1, sel2 = dialogs.create_selection_dialog(image_titles, range(2)) except TypeError: return(None, None) img1 = WindowManager.getImage(image_ids[sel1]) img2 = WindowManager.getImage(image_ids[sel2]) else: IJ.error("You need two images to run the script.") return(None, None) else: IJ.error("You need two images to run the script.") return(None, None) return (img1, img2)
def get_images(minimum=0, maximum=None, exact=None): ''' Returns a list of ImagePlus objects or None if it failed. Passing None as parameter will trigger a dialog to show up to enter the exact number of images. :param minimum: The minimum number of images to select (default: 0). :param maximum: The maximum number of images to select (default: None). :param exact: Set this to get an exact number of images (default: None). ''' if not (minimum or maximum or exact): exact = int(IJ.getNumber("How many images do you want to process?", 3)) def check_count(count): '''Returns True if the count of images is as required.''' # print count, exact, minimum, maximum if exact: if not count == exact: return False else: if minimum: if count < minimum: return False if maximum: if count > maximum: return False return True # Option 1: The selected image is a stack and has the demanded size. if check_count(WindowManager.getCurrentImage().getStackSize()): return stack_to_list_of_imp(WindowManager.getCurrentImage()) # Option 2: The count of open images matches the demanded number. image_ids = WindowManager.getIDList() if exact: if len(image_ids) < exact: return None if len(image_ids) == exact: return [WindowManager.getImage(img_id) for img_id in image_ids] # Option 3: The user can select the images from a list. image_titles = [WindowManager.getImage(id).getTitle() for id in image_ids] img_count = len(image_ids) if exact: img_count = exact elif maximum: img_count = maximum image_titles.append('None') images_selected = dialogs.create_selection_dialog(image_titles, range(img_count), 'Select images for drift correction' ) # dialogs.create_selection_dialog() returns None if canceled if not images_selected: return None # This is only true if 'None has been appended to image_titles and the user selected it. if len(image_ids) in images_selected: images_selected.remove(len(image_ids)) if not check_count(len(images_selected)): return None image_list = [WindowManager.getImage(image_ids[selection]) for selection in images_selected] return image_list