[[Python]] [List of built in macro functions](https://imagej.nih.gov/ij/developer/macro/functions.html) Resources: [link](https://syn.mrc-lmb.cam.ac.uk/acardona/fiji-tutorial/#imglib2-create-img), [link](https://syn.mrc-lmb.cam.ac.uk/acardona/fiji-tutorial/#library), [link](https://learning.rc.virginia.edu/notes/fiji-intro/) ```python from ij import IJ ``` ## Images When you open an image in ImageJ it becomes an "ImagePlus" object ```python imp = IJ.openImage(path) ``` ## Scale calibration ```python from ij.measure import Calibration calibration = Calibration() # new empty calibration calibration.pixelWidth = pixel_width calibration.pixelHeight = pixel_height calibration.setUnit(space_unit) imp.setCalibration(calibration) ``` `space_unit` can be "nm", "um", etc. (will automatically convert um to µm) ### Scale bar macro ```python IJ.run(imp,"Scale Bar...", "width=1000 height=4 font=44 color=Green background=None location=[Lower Right] bold overlay serif") ``` ## Custom text Creating font or color styles: You have to import the Java types first (?) [read more here](https://forum.image.sc/t/text-overlay-in-python-for-imagej/21989/4) Example - Adding text to an image using `TextRoi` ```python from ij.gui import TextRoi from java.awt import Font,Color font = Font("SanSerif", Font.PLAIN, 44) roi = TextRoi(500,1500, "Text",font) ``` ### Font ```python font = Font("SanSerif", Font.PLAIN, 44) ``` 1st argument: Font name? Can be "Arial", "Times New Roman", "ia Writer Mono V", etc. 2nd argument can be `Font.PLAIN`, `Font.ITALIC`, `Font.BOLD`... 3rd argument: int font size ### Color ```python orange = Color(255, 79, 0,255) roi.setStrokeColor(orange) ``` All 4 variables are int 0-255: ```python color = Color(red,green,blue,opacity) ``` ### TextRoi ```python roi = TextRoi(x,y,text,font) ``` `x` and `y` are integer pixel distances, `text` is a string, and `font` is a Font Object ## Overlays You can addd multiple ROIs to an overlay, then overlay the whole group of things on an image: ```python from ij.gui import Roi, Overlay overlay = Overlay() overlay.add(roi1) overlay.add(roi2) imp.setOverlay(overlay) ``` ### Lines ```python from ij.gui import Line line = Line(x1,y1,x2,y2) ``` Makes a line from pixel coordinates `x1,y1` to `x2,y2` ## Script parameters https://imagej.net/scripting/parameters this lets you get parameters from the user in various formats and all the GUI stuff is handled for you