comparison imagej2_math_jython_script.py @ 1:33f6b1f921e7 draft

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