Yuan Yao
Yuan Yao

Reputation: 65

How to use dm script to realize the "save display as" function?

It is easy to save the false color image to BMP or JPG by using "save display as" function in menu. But it fails to save a image stack as the seperated images with the displaying color. "save display as" only saves the front image of the stack. I could not click hundreds of times to save the whole stack! But I do not find the corresponding script function in the dm manual. How to realize it?

Upvotes: 2

Views: 480

Answers (2)

BmyGuest
BmyGuest

Reputation: 2939

The command you're looking for is ImageDisplayGetExportImage() and there is actually an example script in the F1 help documentation of latest GMS for that:

enter image description here


But the command - same as the menu item - will only act on the actual display, so you still need to iterate over the displayed layers by script using ImageDisplaySetDisplayedLayers()

So your script will be something like the following example:

image test:=RealImage("Stack",4,100,100,10)
test=sin(icol/iwidth*Pi()*2) * cos(itheta*iplane) 
test.ShowImage()
imagedisplay disp = test.ImageGetImageDisplay(0)
disp.ImageDisplaySetColorTableByName("Rainbow")

number nz = test.ImageGetDimensionSize(2)
for( number z=0; z<nz; z++){
    disp.ImageDisplaySetDisplayedLayers(z,z)
    imageDisplay newdisp 
    image asDisplayedRGB := disp.ImageDisplayGetExportImage( 7, newdisp ) 
    asDisplayedRGB.SetName( test.GetName() + "_" + z )
    asDisplayedRGB.ShowImage()
}

EGUPerformActionWithAllShownImages("arrange")

Upvotes: 1

miile7
miile7

Reputation: 2393

I hope this is what you are looking for. The shown script allows you to save all images in the current workspace to a directory. You can specify the format, the directory and the name pattern. (It got a bit longer than I expected):

TagGroup formats = NewTagGroup();
formats.TagGroupSetTagAsString("Gatan Format", "dm4");
formats.TagGroupSetTagAsString("Gatan 3 Format", "dm3");
formats.TagGroupSetTagAsString("GIF Format", "gif");
formats.TagGroupSetTagAsString("BMP Format", "bmp");
formats.TagGroupSetTagAsString("JPEG/JFIF Format", "jpg");
formats.TagGroupSetTagAsString("Enhanced Metafile Format", "emf");
formats.TagGroupSetTagAsString("TIFF Format", "tif");
formats.TagGroupSetTagAsString("PCX Format", "pcx");

class FormatDialog : UIFrame{
    TagGroup format_select;
    number FormatDialogGetSelectedFormat(object self){
        if(format_select.TagGroupIsValid()){
            return format_select.DLGGetValue();
        }
        else{
            return -1;
        }
    }
    
    object init(object self){
        TagGroup dlg, dlg_items;
        
        dlg = DLGCreateDialog("Select the format", dlg_items);
        
        dlg_items.DLGAddElement(DLGCreateLabel("Please select the export format"));
        
        format_select = DLGCreateChoice(0);
        format_select.DLGIdentifier("format_select");
        for(number i = 0; i < formats.TagGroupCountTags(); i++){
            string text;
            formats.TagGroupGetIndexedTagAsString(i, text);
            text = formats.TagGroupGetTagLabel(i) + " (" + text + ")";
            format_select.DLGAddChoiceItemEntry(text);
        }
        dlg_items.DLGAddElement(format_select);
        
        self.super.init(dlg);
        return self;
    }
}

object format_dialog = alloc(FormatDialog).init();
if(format_dialog.pose()){
    number format = format_dialog.FormatDialogGetSelectedFormat();
    
    if(format < 0 || format >= formats.TagGroupCountTags()){
        throw("Invalid format is selected");
    }
    
    string save_format = formats.TagGroupGetTagLabel(format);
    string save_extension;
    formats.TagGroupGetIndexedTagAsString(format, save_extension);

    string save_dir;
    if(GetDirectoryDialog("Please select the path to save to", GetApplicationDirectory("open_save", 1), save_dir)){
        string save_name;
        if(GetString("Please set the file name (without extension). A number will be added to the end automatically.", "file_", save_name)){
            for(number i = 0; i < CountImageDocuments(); i++){
                ImageDocument doc = GetImageDocument(i);
                doc.ImageDocumentSaveToFile(save_format, PathConcatenate(save_dir, save_name + i + "." + save_extension));
            }
            OKDialog("Saved " + CountImageDocuments() + " files.");
        }
    }
}

Note that you can add that to DigitalMicrographs menus. Save the posted code as a file, then use File > Install Script File and add the script to any of your existing menus or a new menu.

Install script as menu in Digital Micrograph

Upvotes: 0

Related Questions