Help on a python code calling macro commands
- Dominant language
- Java
- Stars
- 786
- Forks
- 267
- PR merge metrics
- No merged PRs in 30d
Description
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)
Contributor guide
No contributing guide indexed for this repository
Research direction
No repository files or tests are named; start with the Python entry points process_and_copy_structure and process_czi_with_imagej, then reproduce the Fiji invocation with the printed CZI path. Done means the batch run opens each CZI file and writes the requested channel and merged PNG files without the missing-image error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter-notebook, python
- Domain
- computer-vision
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100