comparison imagej2_math_jython_script.py @ 0:b143159845b4 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
author imgteam
date Tue, 17 Sep 2019 17:02:55 -0400
parents
children 5b154339fd90
comparison
equal deleted inserted replaced
-1:000000000000 0:b143159845b4
1 import jython_utils
2 import sys
3 from ij import IJ
4
5 # Fiji Jython interpreter implements Python 2.5 which does not
6 # provide support for argparse.
7 error_log = sys.argv[ -8 ]
8 input = sys.argv[ -7 ]
9 operation = sys.argv[ -6 ]
10 expression = sys.argv[ -5 ]
11 if sys.argv[ -4 ] in [ None, 'None' ]:
12 bin_constant = None
13 else:
14 bin_constant = int( sys.argv[ -4 ] )
15 if sys.argv[ -3 ] in [ None, 'None' ]:
16 float_constant = None
17 else:
18 float_constant = float( sys.argv[ -3 ] )
19 tmp_output_path = sys.argv[ -2 ]
20 output_datatype = sys.argv[ -1 ]
21
22 # Open the input image file.
23 input_image_plus = IJ.openImage( input )
24
25 # Create a copy of the image.
26 input_image_plus_copy = input_image_plus.duplicate()
27 image_processor_copy = input_image_plus_copy.getProcessor()
28 bit_depth = image_processor_copy.getBitDepth()
29
30 try:
31 if operation.find( '_' ) > 0:
32 # Square_Root.
33 new_operation = operation.replace( '_', ' ' )
34 elif operation in [ 'Square', 'Log', 'Exp', 'Abs', 'Reciprocal' ]:
35 # Unfortunately some ImageJ commands require a "..." ending
36 # while others do not. There seems to be no pattern.
37 new_operation = '%s' % operation
38 else:
39 new_operation = '%s...' % operation
40
41 if operation == 'Macro':
42 # Apply the macro code to the image via a call to it's
43 # ImageProcessor since this option does not work using
44 # the IJ.run() method.
45 new_expression = expression.lstrip( '"' ).rstrip( '"' )
46 options = 'code=%s' % new_expression
47 image_processor_copy.applyMacro( new_expression )
48 elif operation == 'Min':
49 # Min does not work without using the ImageProcessor.
50 image_processor_copy.min( float_constant )
51 elif operation == 'Max':
52 # Max does not work without using the ImageProcessor.
53 image_processor_copy.max( float_constant )
54 elif operation == 'Abs':
55 if bit_depth not in [ 16, 32 ]:
56 # Convert the image to 32-bit.
57 IJ.run( input_image_plus_copy, "32-bit", "" )
58 IJ.run( input_image_plus_copy, new_operation, "" )
59 elif operation == 'Reciprocal':
60 if bit_depth != 32:
61 # Convert the image to 32 bit.
62 IJ.run( input_image_plus_copy, "32-bit", "" )
63 IJ.run( input_image_plus_copy, new_operation, "" )
64 else:
65 if operation in [ 'AND', 'OR', 'XOR' ]:
66 # Value is a binary number.
67 options = 'value=%d' % bin_constant
68 elif operation in [ 'Log', 'Exp', 'Square', 'Square_Root' ]:
69 # No constant value.
70 options = ''
71 else:
72 # Value is a floating point number.
73 options = 'value=%.3f' % float_constant
74 IJ.run( input_image_plus_copy, "%s" % new_operation, "%s" % options )
75 # Save the ImagePlus object as a new image.
76 IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path )
77 except Exception, e:
78 jython_utils.handle_error( error_log, str( e ) )