annotate jython_script.py @ 1:8f7beef4f1f3 draft

planemo upload commit 378c3ecfbaed0330faf197811d379431b0f9e92c-dirty
author iuc
date Sun, 11 Oct 2015 13:30:44 -0400
parents 91d82f24657a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
1 import jython_utils
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
2 import sys
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
3 from ij import IJ
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
4
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
5 # Fiji Jython interpreter implements Python 2.5 which does not
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
6 # provide support for argparse.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
7 error_log = sys.argv[ -10 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
8 input = sys.argv[ -9 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
9 threshold_min = float( sys.argv[ -8 ] )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
10 threshold_max = float( sys.argv[ -7 ] )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
11 method = sys.argv[ -6 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
12 display = sys.argv[ -5 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
13 black_background = jython_utils.asbool( sys.argv[ -4 ] )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
14 # TODO: this is not being used.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
15 stack_histogram = jython_utils.asbool( sys.argv[ -3 ] )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
16 tmp_output_path = sys.argv[ -2 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
17 output_datatype = sys.argv[ -1 ]
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
18
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
19 # Open the input image file.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
20 input_image_plus = IJ.openImage( input )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
21
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
22 # Create a copy of the image.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
23 input_image_plus_copy = input_image_plus.duplicate()
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
24 image_processor_copy = input_image_plus_copy.getProcessor()
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
25
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
26 try:
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
27 # Convert image to binary if necessary.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
28 if not image_processor_copy.isBinary():
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
29 # Convert the image to binary grayscale.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
30 IJ.run( input_image_plus_copy, "Make Binary","iterations=1 count=1 edm=Overwrite do=Nothing" )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
31 # Set the options.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
32 if black_background:
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
33 method_str = "%s dark" % method
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
34 else:
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
35 method_str = method
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
36 IJ.setAutoThreshold( input_image_plus_copy, method_str )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
37 if display == "red":
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
38 display_mode = "Red"
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
39 elif display == "bw":
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
40 display_mode = "Black & White"
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
41 elif display == "over_under":
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
42 display_mode = "Over/Under"
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
43 IJ.setThreshold( input_image_plus_copy, threshold_min, threshold_max, display_mode )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
44 # Run the command.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
45 IJ.run( input_image_plus_copy, "threshold", "" )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
46 # Save the ImagePlus object as a new image.
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
47 IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path )
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
48 except Exception, e:
91d82f24657a planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
49 jython_utils.handle_error( error_log, str( e ) )