Skip to content

Empty line in roi txt file causing bug in imagej_roi_converter.py[BUG] #708

@iagopinal

Description

@iagopinal

Describe the bug
The .txt file with the ROIs generates some empty lines that cause an error in line 19 of the imagej_roi_converter.py (xy = map(int, line.rstrip().split(",")))

To Reproduce
I modified slightly the imagej_roi_converter.py to be able to input files programmatically. When it tries to input some .txt files with the rois containing an empty line the map function fails and only the rois before that empty line are reported. It has an easy fix which is to ignore those lines:

if len(line.strip()) == 0:
        continue

If anybody needs to load the rois programmatically, this is the full implementation:

#@String my_file

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import PolygonRoi
from ij.gui import Roi
from java.awt import FileDialog

print(my_file)

RM = RoiManager()
rm = RM.getRoiManager()

imp = IJ.getImage()

textfile = open(my_file, "r")
for line in textfile:
    if len(line.strip()) == 0:
        continue
    print(line)
    xy = list(map(int, line.rstrip().split(",")))
    print(xy)
    X = xy[::2]
    Y = xy[1::2]
    imp.setRoi(PolygonRoi(X, Y, Roi.POLYGON))
    # IJ.run(imp, "Convex Hull", "")
    roi = imp.getRoi()
    print(roi)
    rm.addRoi(roi)
textfile.close()
rm.runCommand("Associate", "true")
rm.runCommand("Show All with labels")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions