Source code

Download source archive ↓

upstream/tools/image_processing/imagej2/imagej2_math_jython_script.py

1import sys
2
3from ij import IJ
4
5# Fiji Jython interpreter implements Python 2.5 which does not
6# provide support for argparse.\
7input_file = sys.argv[-7]
8operation = sys.argv[-6]
9expression = sys.argv[-5]
10if sys.argv[-4] in [None, "None"]:
11    bin_constant = None
12else:
13    bin_constant = int(sys.argv[-4])
14if sys.argv[-3] in [None, "None"]:
15    float_constant = None
16else:
17    float_constant = float(sys.argv[-3])
18tmp_output_path = sys.argv[-2]
19output_datatype = sys.argv[-1]
20
21print("\ninput_file: %s\n" % str(input_file))
22print("\noperation: %s\n" % str(operation))
23print("\nexpression: %s\n" % str(expression))
24print("\nbin_constant: %s\n" % str(bin_constant))
25print("\nfloat_constant: %s\n" % str(float_constant))
26print("\ntmp_output_path: %s\n" % str(tmp_output_path))
27print("\noutput_datatype: %s\n" % str(output_datatype))
28
29# Open the input image file.
30input_image_plus = IJ.openImage(input_file)
31
32# Create a copy of the image.
33input_image_plus_copy = input_image_plus.duplicate()
34image_processor_copy = input_image_plus_copy.getProcessor()
35bit_depth = image_processor_copy.getBitDepth()
36
37if operation.find("_") > 0:
38    # Square_Root.
39    new_operation = operation.replace("_", " ")
40elif operation in ["Square", "Log", "Exp", "Abs", "Reciprocal"]:
41    # Unfortunately some ImageJ commands require a "..." ending
42    # while others do not.  There seems to be no pattern.
43    new_operation = "%s" % operation
44else:
45    new_operation = "%s..." % operation
46
47if operation == "Macro":
48    # Apply the macro code to the image via a call to it's
49    # ImageProcessor since this option does not work using
50    # the IJ.run() method.
51    new_expression = expression.lstrip('"').rstrip('"')
52    options = "code=%s" % new_expression
53    image_processor_copy.applyMacro(new_expression)
54elif operation == "Min":
55    # Min does not work without using the ImageProcessor.
56    image_processor_copy.min(float_constant)
57elif operation == "Max":
58    # Max does not work without using the ImageProcessor.
59    image_processor_copy.max(float_constant)
60elif operation == "Abs":
61    if bit_depth not in [16, 32]:
62        # Convert the image to 32-bit.
63        IJ.run(input_image_plus_copy, "32-bit", "")
64        IJ.run(input_image_plus_copy, new_operation, "")
65elif operation == "Reciprocal":
66    if bit_depth != 32:
67        # Convert the image to 32 bit.
68        IJ.run(input_image_plus_copy, "32-bit", "")
69        IJ.run(input_image_plus_copy, new_operation, "")
70else:
71    if operation in ["AND", "OR", "XOR"]:
72        # Value is a binary number.
73        options = "value=%d" % bin_constant
74    elif operation in ["Log", "Exp", "Square", "Square_Root"]:
75        # No constant value.
76        options = ""
77    else:
78        # Value is a floating point number.
79        options = "value=%.3f" % float_constant
80    IJ.run(input_image_plus_copy, "%s" % new_operation, "%s" % options)
81# Save the ImagePlus object as a new image.
82IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path)