importfile-formatdm-script

Split mrc file into pieces by using DM script


I have a mrc file, which contains 10 pieces. After loading into DM, I can browser the pieces by using the left and right arrow. I'd like to use DM script to read the mrc file and then save each piece into a separated file. Is that possible?


Solution

  • Glad you've solved it yourself by now. In order to have some answer here on StackOverflow as well, this is how I would do it. (Tested with your example file)

    string path = "C:\\temp\\test.mrc"
    
    if ( !OpenDialog( NULL , "Load MRC stack from" , path , path ) ) exit(0)
    
    image stack := NewImageDocumentFromFile(path).ImageDocumentGetRootImage()
    if ( !stack.ImageIsValid() )
        Throw("Could not read file:\n"+path)
        
    //stack.ShowImage()
    if ( 3 != stack.ImageGetNumDimensions() )
        Throw("File is not a 3D stack.")
        
    number sx,sy,sz
    stack.ImageGetDimensionSizes(sx,sy,sz)
    
    string folder = PathExtractDirectory(path,0)
    string baseName = PathExtractBasename(path,0)
    
    // Split and save
    for(number i=0;i<sz;i++){
        
        image img := stack.slice2(0,0,i, 0,sx,1, 1,sy,1).ImageClone()
        string filePath = folder+basename+"_"+i
        OpenAndSetProgressWindow("Saving file "+(i+1)+" / "+sz,filepath,"")
        img.ImageGetOrCreateImageDocument().ImageDocumentSaveToFile( "Gatan Format", filePath)
        Result("\n saved: "+filePath)
    }
    
    CloseProgressWindow()