comparison imagej2_adjust_threshold_binary_jython_script.py @ 3:50f2b961b192 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
author imgteam
date Wed, 25 Sep 2024 16:09:42 +0000
parents bdee06a1bcfa
children
comparison
equal deleted inserted replaced
2:bdee06a1bcfa 3:50f2b961b192
1 import sys 1 import sys
2 2
3 from ij import IJ 3 from ij import IJ, Prefs
4 4
5 # Fiji Jython interpreter implements Python 2.5 which does not 5 # Fiji Jython interpreter implements Python 2.5 which does not
6 # provide support for argparse. 6 # provide support for argparse.
7 error_log = sys.argv[-10] 7 input_file = sys.argv[-8]
8 input_file = sys.argv[-9] 8 threshold_min = float(sys.argv[-7])
9 threshold_min = float(sys.argv[-8]) 9 threshold_max = float(sys.argv[-6])
10 threshold_max = float(sys.argv[-7]) 10 method = sys.argv[-5]
11 method = sys.argv[-6] 11 display = sys.argv[-4]
12 display = sys.argv[-5] 12 black_background = sys.argv[-3] == "yes"
13 black_background = sys.argv[-4] == "yes"
14 # TODO: this is not being used.
15 stack_histogram = sys.argv[-3] == "yes"
16 output_filename = sys.argv[-2] 13 output_filename = sys.argv[-2]
17 output_datatype = sys.argv[-1] 14 output_datatype = sys.argv[-1]
18 15
19 # Open the input image file. 16 # Open the input image file.
20 input_image_plus = IJ.openImage(input_file) 17 input_image_plus = IJ.openImage(input_file)
21 18
22 # Create a copy of the image. 19 # Create a copy of the image.
23 input_image_plus_copy = input_image_plus.duplicate() 20 input_image_plus_copy = input_image_plus.duplicate()
24 image_processor_copy = input_image_plus_copy.getProcessor()
25 21
26 # Convert image to binary if necessary. 22 bit_depth = input_image_plus_copy.getBitDepth()
27 if not image_processor_copy.isBinary(): 23
28 # Convert the image to binary grayscale.
29 IJ.run(
30 input_image_plus_copy,
31 "Make Binary",
32 "iterations=1 count=1 edm=Overwrite do=Nothing",
33 )
34 # Set the options.
35 if black_background: 24 if black_background:
36 method_str = "%s dark" % method 25 Prefs.blackBackground = True
37 else: 26 else:
38 method_str = method 27 Prefs.blackBackground = False
39 IJ.setAutoThreshold(input_image_plus_copy, method_str) 28
29 if method != "Manual":
30 # Set the options.
31 if black_background:
32 method_str = "%s dark" % method
33 suffix = "black"
34 else:
35 method_str = method
36 suffix = ""
37 threshold_min = 1
38 # Set threshold_max based on image bit-depth
39 # For 8-bit images, use 255; for 16-bit images, use 65535
40 if bit_depth == 8:
41 threshold_max = 255 # Default for 8-bit images
42 elif bit_depth == 16:
43 threshold_max = 65535 # Default for 16-bit images
44 else:
45 threshold_max = float('inf') # General fallback if bit depth is unknown
46
47 IJ.setAutoThreshold(input_image_plus_copy, method_str)
48 IJ.run(input_image_plus_copy, "Convert to Mask", "calculate %s" % suffix)
40 if display == "red": 49 if display == "red":
41 display_mode = "Red" 50 display_mode = "Red"
42 elif display == "bw": 51 elif display == "bw":
43 display_mode = "Black & White" 52 display_mode = "Black & White"
44 elif display == "over_under": 53 elif display == "over_under":
45 display_mode = "Over/Under" 54 display_mode = "Over/Under"
46 IJ.setThreshold(input_image_plus_copy, threshold_min, threshold_max, display_mode) 55 IJ.setThreshold(input_image_plus_copy, threshold_min, threshold_max, display_mode)
47 # Run the command.
48 IJ.run(input_image_plus_copy, "threshold", "")
49 # Save the ImagePlus object as a new image. 56 # Save the ImagePlus object as a new image.
50 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) 57 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)