Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active June 13, 2024 08:16
Show Gist options
  • Save lacan/6b1c80b6113a6fba53c4cda64942e9d3 to your computer and use it in GitHub Desktop.
Save lacan/6b1c80b6113a6fba53c4cda64942e9d3 to your computer and use it in GitHub Desktop.
[RoiManager to SVG] Converts simple polygon ROIs from the ROI Manager to an SVG file #fiji #svg #roi-to-svg #conversion #imagej #macro
// Reference: https://forum.image.sc/t/basic-macro-help-rois-to-svg/7861/3
#@ File svgFile ("style=save")
svg = "<?xml version=\"1.0\" ?><svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:jfreesvg='http://www.jfree.org/jfreesvg/svg' width='"+getWidth()+"' height='"+getHeight()+"'>\n";
n = roiManager("count");
for (j = 0; j < n; j++){
roiManager("select", j);
getSelectionCoordinates(x, y);
text = "<path d='m";
x2 = y2 = 0;
for (i = 0; i < x.length; i++) {
text += " " + (x[i] - x2) + "," + (y[i] - y2);
x2 = x[i];
y2 = y[i];
}
text += " z' />\n";
svg += text;
}
svg += "</svg>";
File.saveString(svg, svgFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment