How I can save image

Luc

Member
Hi,
I'm writring a new script to cropt an image.

...
var littleImage = new Image(img);
littleImage .cropTo( 200,200,300,300);
....

How I can save this image?

Thanks
 
Hi Luc,

Here is a basic function to write an image to a disk file:

JavaScript:
function writeImage( filePath, image )
{
   let suffix = File.extractSuffix( filePath );
   let F = new FileFormat( suffix, false/*toRead*/ , true/*toWrite*/ );
   if ( F.isNull )
      throw new Error( "No installed file format can write " + suffix + " files." );

   let f = new FileFormatInstance( F );
   if ( f.isNull )
      throw new Error( "Unable to instantiate file format: " + F.name );

   if ( !f.create( filePath ) )
      throw new Error( "Error creating output file: " + filePath );

   let d = new ImageDescription;
   d.bitsPerSample = image.bitsPerSample;
   d.ieeefpSampleFormat = image.isReal;
   if ( !f.setOptions( d ) )
      throw new Error( "Unable to set output file options: " + filePath );

   if ( !f.writeImage( imageWindow.mainView.image ) )
      throw new Error( "Error writing output file: " + filePath );
}

You can call this function to save any image that you have generated or processed in your code; for example:

...
var littleImage = new Image( img );
littleImage.cropTo( 200, 200, 300, 300 );
....
writeImage( "/path/to/foobar.xisf", littleImage );


This is just a basic example. You can sophisticate it more to save image properties, FITS keywords, use output hints, etc. I hope this helps.
 
Hi Luc,

Here is a basic function to write an image to a disk file:

JavaScript:
function writeImage( filePath, image )
{
   let suffix = File.extractSuffix( filePath );
   let F = new FileFormat( suffix, false/*toRead*/ , true/*toWrite*/ );
   if ( F.isNull )
      throw new Error( "No installed file format can write " + suffix + " files." );

   let f = new FileFormatInstance( F );
   if ( f.isNull )
      throw new Error( "Unable to instantiate file format: " + F.name );

   if ( !f.create( filePath ) )
      throw new Error( "Error creating output file: " + filePath );

   let d = new ImageDescription;
   d.bitsPerSample = image.bitsPerSample;
   d.ieeefpSampleFormat = image.isReal;
   if ( !f.setOptions( d ) )
      throw new Error( "Unable to set output file options: " + filePath );

   if ( !f.writeImage( imageWindow.mainView.image ) )
      throw new Error( "Error writing output file: " + filePath );
}

You can call this function to save any image that you have generated or processed in your code; for example:

...
var littleImage = new Image( img );
littleImage.cropTo( 200, 200, 300, 300 );
....
writeImage( "/path/to/foobar.xisf", littleImage );


This is just a basic example. You can sophisticate it more to save image properties, FITS keywords, use output hints, etc. I hope this helps.

Great!!!! Thanks a lots
 
Back
Top