code: def image_write(image, filename, ri=False):
      
      if filename.endswith('.npy'):
          img_array = image.numpy()
          img_header = {'origin': image.origin,'spacing': image.spacing,
                          'direction': image.direction.tolist(), 'components': image.components}
  
          np.save(filename, img_array)
          with open(filename.replace('.npy','.json'), 'w') as outfile:
              json.dump(img_header, outfile)
      else:
          image.to_file(filename)
  
      if ri:
          return image
docs: Write an ANTsImage to file
  
      ANTsR function: `antsImageWrite`
  
      Arguments
      ---------
      image : ANTsImage
          image to save to file
  
      filename : string
          name of file to which image will be saved
  
      ri : boolean
          if True, return image. This allows for using this function in a pipeline:
              >>> img2 = img.smooth_image(2.).image_write(file1, ri=True).threshold_image(0,20).image_write(file2, ri=True)
          if False, do not return image