exportsaving-datadm-script

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


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 ) 

Solution

  • 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):


    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);