comparison jython_utils.py @ 0:dc3935a0165a 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 16:57:52 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dc3935a0165a
1 import imagej2_base_utils
2 from ij import IJ
3
4 IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES = { '0':'GRAY8', '1':'GRAY16', '2':'GRAY32',
5 '3':'COLOR_256', '4':'COLOR_RGB' }
6
7 def asbool( val ):
8 return str( val ).lower() in [ 'yes', 'true' ]
9
10 def convert_before_saving_as_tiff( image_plus ):
11 # The bUnwarpJ plug-in produces TIFF image stacks consisting of 3
12 # slices which can be viewed in ImageJ. The 3 slices are: 1) the
13 # registered image, 2) the target image and 3) the black/white warp
14 # image. When running bUnwarpJ from the command line (as these
15 # Galaxy wrappers do) the initial call to IJ.openImage() (to open the
16 # registered source and target images produced by bUnwarpJ) in the
17 # tool's jython_script.py returns an ImagePlus object with a single
18 # slice which is the "generally undesired" slice 3 discussed above.
19 # However, a call to IJ.saveAs() will convert the single-slice TIFF
20 # into a 3-slice TIFF image stack (as described above) if the selected
21 # format for saving is TIFF. Galaxy supports only single-layered
22 # images, so to work around this behavior, we have to convert the
23 # image to something other than TIFF so that slices are eliminated.
24 # We can then convert back to TIFF for saving. There might be a way
25 # to do this without converting twice, but I spent a lot of time looking
26 # and I have yet to discover it.
27 tmp_dir = imagej2_base_utils.get_temp_dir()
28 tmp_out_png_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'png' )
29 IJ.saveAs( image_plus, 'png', tmp_out_png_path )
30 return IJ.openImage( tmp_out_png_path )
31
32 def get_binary_options( black_background, iterations=1, count=1, pad_edges_when_eroding='no' ):
33 options = [ 'edm=Overwrite', 'iterations=%d' % iterations, 'count=%d' % count ]
34 if asbool( pad_edges_when_eroding ):
35 options.append( 'pad' )
36 if asbool( black_background ):
37 options.append( "black" )
38 return " ".join( options )
39
40 def get_display_image_type( image_type ):
41 return IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES.get( str( image_type ), None )
42
43 def handle_error( error_log, msg ):
44 # Java writes a lot of stuff to stderr, so the received error_log
45 # will log actual errors.
46 elh = open( error_log, 'wb' )
47 elh.write( msg )
48 elh.close()