# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not # work for some features. The following creates a symlink with an appropriate file # extension that points to the Galaxy dataset. This symlink is used by ImageJ. tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) # Define command response buffers. tmp_out = tempfile.NamedTemporaryFile().name tmp_stdout = open( tmp_out, 'wb' ) tmp_err = tempfile.NamedTemporaryFile().name tmp_stderr = open( tmp_err, 'wb' ) # Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. error_log = tempfile.NamedTemporaryFile( delete=False ).name # Build the command line. cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) if cmd is None: imagej2_base_utils.stop_err( "ImageJ not found!" ) cmd += ' %s' % error_log cmd += ' %s' % tmp_input_path cmd += ' %.3f' % args.threshold_min cmd += ' %.3f' % args.threshold_max cmd += ' %s' % args.method cmd += ' %s' % args.display cmd += ' %s' % args.black_background cmd += ' %s' % args.stack_histogram cmd += ' %s' % tmp_output_path cmd += ' %s' % args.output_datatype # Run the command. proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) rc = proc.wait() # Handle execution errors. if rc != 0:
tmp_dir = imagej2_base_utils.get_temp_dir() source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) source_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_elastic_transformation, 'txt' ) target_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_raw_transformation, 'txt' ) # Define command response buffers. tmp_out = tempfile.NamedTemporaryFile().name tmp_stdout = open( tmp_out, 'wb' ) tmp_err = tempfile.NamedTemporaryFile().name tmp_stderr = open( tmp_err, 'wb' ) # Build the command line to compose the raw and elastic transformations. cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) if cmd is None: imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) cmd += ' -compose_raw_elastic' # Target is sent before source. cmd += ' %s' % target_image_path cmd += ' %s' % source_image_path cmd += ' %s' % target_raw_transformation_path cmd += ' %s' % source_elastic_transformation_path cmd += ' %s' % args.output # Compose the raw and elastic transformations into another raw transformation using bUnwarpJ. proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) rc = proc.wait() if rc != 0: error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) imagej2_base_utils.stop_err( error_message )
#!/usr/bin/env python import argparse import imagej2_base_utils import imagej2_utils if __name__=="__main__": # Parse Command Line. parser = argparse.ArgumentParser() parser.add_argument( '--in_fname', dest='in_fname', help='Path to the input file' ) parser.add_argument( '--input_datatype', dest='input_datatype', help='Input image datatype' ) parser.add_argument( '--output_datatype', dest='output_datatype', help='Output image datatype' ) parser.add_argument( '--out_fname', help='Path to the output file' ) args = parser.parse_args() # Start the JVM via the Javabridge. imagej2_utils.start_vm( args=None, class_path=None, max_heap_size=None, run_headless=True ) try: tmp_dir = imagej2_base_utils.get_temp_dir() in_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.in_fname, args.input_datatype ) # Load the input image. image, scale = imagej2_utils.load_image( in_image_path, rescale=False, wants_max_intensity=True ) # Write the output image. out_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) imagej2_utils.write_image( image_path=out_image_path, pixels=image, pixel_type=str( image.dtype ), move_to=args.out_fname ) except Exception, e: imagej2_base_utils.stop_err( str( e ) ) finally: imagej2_utils.kill_vm() imagej2_base_utils.cleanup_before_exit( tmp_dir )
dest='output_datatype', help='Output image datatype') parser.add_argument('--out_fname', help='Path to the output file') args = parser.parse_args() # Start the JVM via the Javabridge. imagej2_utils.start_vm(args=None, class_path=None, max_heap_size=None, run_headless=True) try: tmp_dir = imagej2_base_utils.get_temp_dir() in_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.in_fname, args.input_datatype) # Load the input image. image, scale = imagej2_utils.load_image(in_image_path, rescale=False, wants_max_intensity=True) # Write the output image. out_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype) imagej2_utils.write_image(image_path=out_image_path, pixels=image, pixel_type=str(image.dtype), move_to=args.out_fname) except Exception, e: imagej2_base_utils.stop_err(str(e)) finally: imagej2_utils.kill_vm() imagej2_base_utils.cleanup_before_exit(tmp_dir)