imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
513 stars 218 forks source link

Help on a python code calling macro commands #231

Open JHartig opened 6 months ago

JHartig commented 6 months ago

Hey guys. I am relatively unfamiliar with the imagej macro language and I've got a code that seems to have a problem with the calling the images that i want to batch process. Anyone got any idea of why it doesnt work? The error log from imagej (which opens when running it via a jupyter notebook) says the image doesnt exist, however its there and i guess its rather a problem with the path/mca

import os import shutil import subprocess from tqdm import tqdm

Function to process and save a CZI file with ImageJ commands

def process_czi_with_imagej(input_directory, output_directory, czi_file): czi_file_path = os.path.join(input_directory, czi_file)

# Create the output subfolder in the correct structure
relative_path = os.path.relpath(os.path.dirname(czi_file_path), input_directory)
output_subfolder = os.path.join(output_directory, relative_path)

# Define the path to your Fiji (ImageJ) executable
fiji_executable = r"C:\Users\Gastkonto\fiji-win64\Fiji.app\ImageJ-win64.exe"  # Replace with the actual path

# Print czi file path
print("CZI File Path:", czi_file_path)

# Define the ImageJ Macro commands
imagej_commands = [
    'run("Bio-Formats Importer", "open=[' + czi_file_path.replace("\\", "/") + '] autoscale color_mode=Composite");',
    'run("8-bit");',
    'run("Split Channels");',
    'selectWindow("C1-" + getTitle()); run("Colors...", "channels=1 stops=Blue");',
    'selectWindow("C2-" + getTitle()); run("Colors...", "channels=1 stops=Yellow");',
    'selectWindow("C3-" + getTitle()); run("Colors...", "channels=1 stops=Magenta");',
    'selectWindow("C4-" + getTitle()); run("Colors...", "channels=1 stops=Green");',
    'run("Images to Stack", "name=Stack title=[] use");',
    'run("Z Project...", "projection=[Max Intensity] all");',
    'run("Channels Tool...");',
    'run("RGB Color");',
    'run("Split Channels");',
    'selectWindow("C1-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C2-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C3-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C4-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C1-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_1.png') + '");',
    'selectWindow("C2-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_2.png') + '");',
    'selectWindow("C3-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_3.png') + '");',
    'selectWindow("C4-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_4.png') + '");',
    'run("Merge Channels...", "c1=C1-" + getTitle() + " c2=C3-" + getTitle() + " create");',
    'saveAs("PNG", "' + os.path.join(output_subfolder, 'merged_1.png') + '");',
    'run("Merge Channels...", "c1=C1-" + getTitle() + " c2=C2-" + getTitle() + " c3=C3-" + getTitle() + " create");',
    'saveAs("PNG", "' + os.path.join(output_subfolder, 'merged_2.png') + '");',
    'selectWindow("C1-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C2-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C3-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C4-" + getTitle()); run("Brightness/Contrast...", "reset");',
    'selectWindow("C1-" + getTitle()); run("Brightness/Contrast...", "auto");',
    'selectWindow("C2-" + getTitle()); run("Brightness/Contrast...", "auto");',
    'selectWindow("C3-" + getTitle()); run("Brightness/Contrast...", "auto");',
    'selectWindow("C4-" + getTitle()); run("Brightness/Contrast...", "auto");',
    'selectWindow("C1-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_1_auto.png') + '");',
    'selectWindow("C2-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_2_auto.png') + '");',
    'selectWindow("C3-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_3_auto.png') + '");',
    'selectWindow("C4-" + getTitle()); saveAs("PNG", "' + os.path.join(output_subfolder, 'channel_4_auto.png') + '");',
    'run("Merge Channels...", "c1=C1-" + getTitle() + " c2=C3-" + getTitle() + " create");',
    'saveAs("PNG", "' + os.path.join(output_subfolder, 'merged_1_auto.png') + '");',
    'run("Merge Channels...", "c1=C1-" + getTitle() + " c2=C2-" + getTitle() + " c3=C3-" + getTitle() + " create");',
    'saveAs("PNG", "' + os.path.join(output_subfolder, 'merged_2_auto.png') + '");',
    'close();'
]

 # Construct the full command
full_command = [fiji_executable, "--console", "-eval", ' '.join(imagej_commands)]

# Print the full ImageJ command before executing it
print("Full ImageJ Command:", ' '.join(full_command))

# Launch Fiji (ImageJ) and run the commands, capture output and error
result = subprocess.run(full_command, capture_output=True, text=True)

# Print output and error information
print(result.stdout)
print(result.stderr)

Function to copy directory structure and process CZI files

def process_and_copy_structure(input_directory, output_directory):

Walk through the input directory

for root, dirs, files in os.walk(input_directory):
    # Construct the corresponding path in the output directory
    relative_path = os.path.relpath(root, input_directory)
    output_path = os.path.join(output_directory, relative_path)

    # Create the corresponding subfolder in the output directory
    os.makedirs(output_path, exist_ok=True)

    # Process each CZI file in the current subfolder
    for file in tqdm(files, desc=f"Processing {relative_path}", unit="file"):
        if file.endswith(".czi"):
            process_czi_with_imagej(input_directory, output_directory, file)

Example usage

input_directory = r"xxx" output_directory = r"xxx"

process_and_copy_structure(input_directory, output_directory)