How to set an ImageDocument to be not dirty in dm-script

164 views Asked by At

How do I set an ImageDocument not to be dirty anymore in python without saving?


I have the python code posted below which can be represented by the following dm-script code.

String file_path = GetApplicationDirectory(0, 1).PathConcatenate("test-image.dm4");
Image img := realimage("test", 4, 64, 64);

ImageDocument doc = img.ImageGetOrCreateImageDocument();
doc.ImageDocumentSaveToFile("Gatan Format", file_path);
doc.ImageDocumentShowAtRect(100, 100, 164, 164);

The (python code below) creates and shows an ImageDocument. The image is saved already. But even saving it directly in DigitalMicrograph with its own module it does not recognize that it is saved. I can link the file manually (by executing dm-script code from python) but I cannot tell the program that the images are not modified.

There is a function ImageDocumentIsDirty(). But this function only returns whether the image is modified or not. I cannot set it.

My program creates a new workspace and loads more than 100 images. When closing DigitalMicrograph, it asks for every single of the 100 images if it should be saved. I cannot leave the user with 100 times clicking No. Especially because the files are saved.

So, how do I tell that the image is saved already?

try:
    import DigitalMicrograph as DM
    import numpy as np
    import execdmscript
    import os

    name = "Test image"
    file_path = os.path.join(os.getcwd(), "test-image.dm4")
    
    # create image
    image_data = np.random.random((64, 64))
    image = DM.CreateImage(image_data)
    image.SetName(name)
    
    # create, save and show image document
    image_doc = image.GetOrCreateImageDocument()
    image_doc.SetName(name)
    image_doc.SaveToFile("Gatan Format", file_path)
    print("Saving image to", file_path)
    image_doc.ShowAtRect(100, 100, 164, 164)
    
    # link the image to the file
    dmscript = "\n".join((
        "for(number i = CountImageDocuments() - 1; i >= 0; i--){",
            "ImageDocument img_doc = GetImageDocument(i);",
            "if(img_doc.ImageDocumentGetName() == name){",
                "img_doc.ImageDocumentSetCurrentFile(path);",
                "break;",
            "}",
        "}"
    ))

    svars = {
        "name": image_doc.GetName(),
        "path": file_path
    }

    with execdmscript.exec_dmscript(dmscript, setvars=svars):
        pass
except Exception as e:
    print("{}: ".format(e.__class__.__name__), e)
    import traceback
    traceback.print_exc()
1

There are 1 answers

1
BmyGuest On BEST ANSWER

the command you're looking for is void ImageDocumentClean( ImageDocument imgDoc )

as in

image img := realimage("test",4,100,100)
img.ShowImage()
imageDocument doc = img.ImageGetOrCreateImageDocument()

Result("\n Dirty? " + doc.ImageDocumentIsDirty())
doc.ImageDocumentClean()
Result("\n Dirty? " + doc.ImageDocumentIsDirty())

Also: The reason it becomes dirty in a first place is, that window-positions are stored as part of the document. (Other things, like tags, could also apply.)