annotate imagej2_enhance_contrast_jython_script.py @ 1:97a50ff3df5d draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
author iuc
date Fri, 22 Jul 2016 23:17:48 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
1 import jython_utils
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
2 import sys
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
3 from ij import IJ
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
4
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
5 # Fiji Jython interpreter implements Python 2.5 which does not
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
6 # provide support for argparse.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
7 error_log = sys.argv[ -7 ]
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
8 input = sys.argv[ -6 ]
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
9 equalize_histogram = jython_utils.asbool( sys.argv[ -5 ] )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
10 saturated_pixels = sys.argv[ -4 ]
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
11 normalize = jython_utils.asbool( sys.argv[ -3 ] )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
12 tmp_output_path = sys.argv[ -2 ]
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
13 output_datatype = sys.argv[ -1 ]
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
14
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
15 # Open the input image file.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
16 input_image_plus = IJ.openImage( input )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
17
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
18 # Create a copy of the image.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
19 input_image_plus_copy = input_image_plus.duplicate()
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
20 image_processor_copy = input_image_plus_copy.getProcessor()
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
21 bit_depth = image_processor_copy.getBitDepth()
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
22
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
23 # Set the options
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
24 options = []
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
25 # If equalize_histogram, saturated_pixels and normalize are ignored.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
26 if equalize_histogram:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
27 options.append( 'equalize' )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
28 else:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
29 if saturated_pixels not in [ None, 'None' ]:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
30 # Fiji allows only a single decimal place for this value.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
31 options.append( 'saturated=%.3f' % float( saturated_pixels ) )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
32 # Normalization of RGB images is not supported.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
33 if bit_depth != 24 and normalize:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
34 options.append( 'normalize' )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
35 try:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
36 # Run the command.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
37 options = "%s" % ' '.join( options )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
38 IJ.run( input_image_plus_copy, "Enhance Contrast...", options )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
39 # Save the ImagePlus object as a new image.
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
40 IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path )
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
41 except Exception, e:
97a50ff3df5d planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8ea6a4271431c05c82b09c0d3e629b13e6ea7936
iuc
parents:
diff changeset
42 jython_utils.handle_error( error_log, str( e ) )