How to convert a .dm3 file (with annotation and scale bar) to .jpg/jpeg image?

Viewed 182

I wonder how to convert a dm3 file into .jpg/jpeg images? there is test annotation and scale bar on the image. I setup a script but it always show that "the format cannot contain the data to be saved". This can be done via file/batch convert function. So how to realize the same function in script? Thanks

image test:=IntegerImage("test",2,1,100,100)
test.ShowImage()
image frontimage:=GetFrontImage()
string  filename=getname(frontimage)
imagedisplay disp = frontImage.ImageGetImageDisplay(0)
disp.applydatabar()
ImageDocument frontDoc = GetFrontImageDocument() 
string directoryname, pathname
number length
if(!SaveAsDialog("","Do Not Change Me",directoryname)) exit(0)
length=len(directoryname)-16 
directoryname=mid(directoryname,0,length) 
pathname=directoryname+filename
frontDoc.ImageDocumentSaveToFile( "JPG Format", pathname ) 
2 Answers

To convert to jpg you have to use "JPEG/JFIF Format" as the handler (=format).

It has to be exactly this string in the ImageDocument.ImageDocumentSaveToFile() function. Other formats are mentioned in the help (F1 > Scripting > Objects > Document Object Model > ImageDocument Object > ImageDocumentSaveToFile() function). Those are (for example):

  • 'Gatan Format'
  • 'Gatan 3 Format'
  • 'GIF Format'
  • 'BMP Format'
  • 'JPEG/JFIF Format'
  • 'Enhanced Metafile Format'

In your code you are using the SaveAsDialog() to get a directory. This is not necessary. You can use GetDirectoryDialog() to get a directory. This saves you the name operation for the directoryname and avoids problems when users do change your filename.

Also for concatinating paths I prefer using PathConcatenate(). On the first hand this makes your code a lot more readable since its name tells what you are doing. On the other hand this also takes care of the directory ending with \ or not and other path related things.


The following code is what I think you need:

Image test := IntegerImage("test", 2, 1, 100, 100);
test.ShowImage();

Image frontimage := GetFrontImage();

ImageDisplay disp = frontImage.ImageGetImageDisplay(0);
disp.applydatabar();

ImageDocument frontDoc = GetFrontImageDocument();

string directoryname;
if(!GetDirectoryDialog("Select directory", "C:\\\\", directoryname)){
    //                                        ↑
    // You can of course use something else as the start point for selection here
    exit(0);
}

string filename = GetName(frontimage);
string pathname = directoryname.PathConcatenate(filename);

frontDoc.ImageDocumentSaveToFile("JPEG/JFIF Format", pathname);

This answer is correct and should be accepted. Your problem is the wrong file-type string. You want to use "JPEG/JFIF Format"


A bit more general information on image file saving in DigitalMicrograph.

  • One doesn't save images but always imageDocuments that can contain one, more, or even zero image objects in them. Script-commands that save an image like SaveAsGatan() really just call things like: ImageGetOrCreateImageDocument().ImageDocumentSaveToFile()

    The difference doesn't really matter for simple one-image-in-document type images, but it can make a difference when there are multiple images in a document, or when a single image is displayed multiple times simultaneously (which can be done.) So it is always good to know what "really" goes on.

  • ImageDocuments contain some properties relating to saving:

    • A save format (“Gatan Format”, “TIFF Format”, …)
      • Default value: What it was opened with, or last used save-format in case of creation
      • Script commands:
        ImageDocumentGetCurrentFileSaveFormat()
        ImageDocumentSetCurrentFileSaveFormat()
    • A current file path:
      • Default value: What it was opened from, or empty
      • Script commands:
        ImageDocumentGetCurrentFile()
        ImageDocumentSetCurrentFile()
    • A dirty-state:
      • Default value: clean when opened, dirty when created
      • Script commands:
        ImageDocumentIsDirty()
        ImageDocumentClean()
    • A linked-to-file state:
      • Default value: true when opened, false when created
      • Script commands:
        ImageDocumentIsLinkedToFile()
  • There are two ways of saving an imageDocument:

    • Saving the current document itself to disc:

      void ImageDocumentSave( ImageDocument imgDoc, Number save_style )

      This utilizes the current properties of the imageDocument to save it to current path in current format, marking it clean in the process.
      The save_style parameter determines how the program deals with missing info:

      • 0 = never ask for path
      • 1 = ask if not linked (or empty path)
      • 2 = always ask
    • Saving a copy of the current document to disc:

      void ImageDocumentSaveToFile( ImageDocument imgDoc, String handler, String fileName )

      This makes a copy and save the file under provided path in the provided format. The imageDocument in memory does not change its properties. Most noticeable: It does not become clean, and it is not linked to the provided file on disc.
      The filename parameter specifies the saving location including the filename. If a file extension is provided, it has to match the file-format, but it can be left out.
      The handler parameter specified the file-format and can be anything GMS currently supports, such as:

      • Gatan Format
      • Gatan 3 Format
      • GIF Format
      • BMP Format
      • JPEG/JFIF Format
      • Enhanced Metafile Format

In short:

To save the currently opened imageDocument with a different format, you would want to do:

imageDocument doc = GetFrontImageDocument()
doc.ImageDocumentSetCurrentFileSaveFormat("TIFF Format")
doc.ImageDocumentSave(0)

While to just save a copy of the current state you would use:

imageDocument doc = GetFrontImageDocument()
string path = doc.ImageDocumentGetCurrentFile() // full path including extension!
path = PathExtractDirectory(path,0) + PathExtractBaseName(path,0) // path without file extension
doc.ImageDocumentSaveToFile("TIFF Format", path )
Related