Source code

Download source archive ↓

upstream/tools/image_processing/imagej2/imagej2_enhance_contrast_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 = sys.argv[-6]
8equalize_histogram = sys.argv[-5] == "yes"
9saturated_pixels = sys.argv[-4]
10normalize = sys.argv[-3] == "yes"
11tmp_output_path = sys.argv[-2]
12output_datatype = sys.argv[-1]
13
14# Open the input image file.
15input_image_plus = IJ.openImage(input)
16
17# Create a copy of the image.
18input_image_plus_copy = input_image_plus.duplicate()
19image_processor_copy = input_image_plus_copy.getProcessor()
20bit_depth = image_processor_copy.getBitDepth()
21
22# Set the options
23options = []
24# If equalize_histogram, saturated_pixels and normalize are ignored.
25if equalize_histogram:
26    options.append("equalize")
27else:
28    if saturated_pixels not in [None, "None"]:
29        # Fiji allows only a single decimal place for this value.
30        options.append("saturated=%.3f" % float(saturated_pixels))
31    # Normalization of RGB images is not supported.
32    if bit_depth != 24 and normalize:
33        options.append("normalize")
34# Run the command.
35options = "%s" % " ".join(options)
36IJ.run(input_image_plus_copy, "Enhance Contrast...", options)
37# Save the ImagePlus object as a new image.
38IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path)