Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
// Insert Scale Bar Script
// 2023-12-13 by Hannah Mette and Lion Raaz (raaz@molgen.mpg.de)
// This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
// (CC BY-SA 4.0) https://creativecommons.org/licenses/by-sa/4.0/
// available under https://github.molgen.mpg.de/raaz/measure_nuclei
// script to
// invert a selected channel
// auto-adjust brightness (if chosen)
// insert a scale bar
// in a zeiss .czi microscopy image
// prevent ImageJ from showing the processing steps during
// processing of a large number of images, speeding up the macro
setBatchMode(true);
//Orner auswählen
// Show the user a dialog to select a directory of images (auswählen des Ordners)
inputDirectory = getDirectory("Choose a Directory of Images");
// Get the list of files from that directory(ist nötig, damit der ausgewählte Ordner einer Variablen zugeordnet ist, sonst könnte Fiji nichts damit anfangen)
fileList = getFileList(inputDirectory);
//Channel auswählen
// Show user dialog to select: Channel (C1-C5), Invert (yes/no), ScaleBar (yes/no), design of Scale Bar, AutoAdjust (yes/no)
//first part creates new variables (everything we want to be able to change and select)
Dialog.create("A tännschn please");
channels = newArray("C1", "C2", "C3", "C4", "C5");
colors = newArray("White", "Black");
doinvert = true;
autoAdjust = false;
ScaleBAr = true;
width = 100;
height = 100;
thickness = 50;
font = 80;
color = "Black bold overlay";
output_suffix = "_scalebar";
//Dialog.addChoice/Number/checkbox/string then adds these varibles to the dialog from which the user can choose
//(this is why the variables had to be definded beforehand)
Dialog.addMessage("Please rename the output file if desired and select which channel is to be analysed.");
Dialog.addChoice("Channel:", channels, "C3");
Dialog.addCheckbox("Invert Image", true);
Dialog.addCheckbox("Auto adjust brightness", true);
Dialog.addCheckbox("ScaleBar ", true);
Dialog.addNumber("Width:", width);
Dialog.addNumber("Height:", height);
Dialog.addNumber("Thickness:", thickness);
Dialog.addNumber("Font:", font);
Dialog.addChoice("Color:", colors, "Black");
Dialog.addString("Output suffix:", output_suffix, 30);
Dialog.show();
//Again, this is to connect the varible to its command in the dialog
channel = Dialog.getChoice();
doinvert = Dialog.getCheckbox();
autoAdjust = Dialog.getCheckbox();
ScaleBAr = Dialog.getCheckbox();
width = Dialog.getNumber();
height = Dialog.getNumber();
thickness = Dialog.getNumber();
font = Dialog.getNumber();
color = Dialog.getChoice();
output_suffix = Dialog.getString();
//This is just to see if the Dialog works and the varibles are connected to the right commands
//print("dialog input");
//print(channel);
//print(doinvert);
//print(autoAdjust);
//print(ScaleBAr);
//print(width);
//print(height);
//print(thickness);
//print(font);
//print(color);
//print(output_suffix);
//processImage(fileList[1]);
//Loop über alle Bilder:
for (i = 0; i < fileList.length; i++)
{
if (fileIsImage(fileList[i])) {
processImage(fileList[i]);
}
}
//only select .czi files, important if their are other files in the folder e.g already processed one
function fileIsImage(imageFile) {
// check if file is .czi
if (endsWith(imageFile, ".czi")){
return(1);
} else {
return(0);
}
}
//open Image (Greyscale)
function processImage(imageFile) {
// process selected channel
run("Bio-Formats", "open=[" + inputDirectory + imageFile + "] autoscale color_mode=Grayscale rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
//get the filename to be able to save the processed image with it
filename = getTitle();
//Split channels so only the selected channel is being processed
run("Split Channels");
//channel + "-" + filename is how the channels are named in Fiji, this way the channel that was selected in the beginning is being processed
selectWindow(channel + "-" + filename);
//if the box for autoAdjust was checked, the commands will run, if not they wont (same for Invert and ScaleBar)
if (autoAdjust) {
run("Enhance Contrast", "saturated=0.35");
run("Apply LUT");
}
if (doinvert) {
run("Invert");
}
if (ScaleBAr) {
run("Scale Bar...", "width=" + width + " height=" + height + " thickness=" + thickness + " font=" + font + " color=" + color + " bold overlay");
}
saveAs("PNG", inputDirectory + filename + output_suffix + ".png");
close("*"); // Closes all images
}