将 32 位浮点数 DM4 转换为带符号的 16 位

Converting 32bit float DM4s to signed 16bit

我有一堆 cryo 和 liquid cell dm4 图像,它们是 32 位浮点数,但是 32 位的精度和大值是完全没有必要的,所以我们决定将它们转换为 16 位有符号整数。

我需要保留 dm4 图像的元数据结构,因为图像仍然需要在数字显微照片中打开。所以使用 hyperspy 或 ncempy 不是一种选择,因为它们不能写入 dm4 文件。

我目前在 dm-script 中有一个脚本可以执行此操作,但它一次只能进入一个目录,并且不知道如何处理液体细胞数据。而且我还不够好。

我想知道我是否可以在 DM 的 python 界面中做同样的事情?因为我可以操纵文件结构并使用 python.

轻松遍历目录

我指的 dm 脚本是 FELMI ZFE DigitalMicrograph Script Database 稍微调整为允许有符号整数而不创建 tiff,因为它们目前对我们没有用。

编辑:dm-script 目前运行良好,我很好奇是否有办法将源和输出目录从 python 脚本传递到我的 dm-script。这样我就可以在 python 中完成所有目录处理,并且一次只调用一个文件夹的 dm-script。

不确定这是否是您想要的,但下面的脚本显示了如何从所有子文件夹递归构建 file-list,按扩展名过滤它,然后对每个子文件夹进行操作。

当前脚本将打开给定文件夹及其子文件夹的所有 .dm4 文件,将图像转换为 sint16 并使用前缀名称重新保存。

不过,您可以很容易地通过修改方法ActOnFile来调整脚本。

// Script class compiling a (filtered) TagList of filepaths from 
// a folder. Optionally with recursion for subfolders
Class CRecursiveFileList{
    TagGroup BuildList( object self, string path, string leadIn, string leadOut, number bSubfolders){
        tagGroup FileList = NewTagList()
        TagGroup allFiles = GetFilesInDirectory( path, 1 )
        number nFiles = allFiles.TagGroupCountTags()
        leadIn = StringToLower(leadIn)
        leadOut = StringToLower(leadOut)
        for ( number i=0; i<nFiles; i++ )
        {
            string file
            TagGroup entry
            allFiles.TagGroupgetIndexedTagAsTagGroup( i, entry )
            entry.TagGroupGetTagAsString( "Name", file )
            file = StringToLower(file)
            
            if ( len(leadIn)  > len(file) ) continue
            if ( len(leadOut) > len(file) ) continue
            if ( left(file,len(leadIn)) != leadIn ) continue
            if ( right(file,len(leadOut)) != leadOut ) continue
            
            FileList.TagGroupInsertTagAsString( Infinity(), PathConcatenate(path,file) )
        }
        if (bSubFolders)
        {
            TagGroup allFolders = GetFilesInDirectory( path, 2 )
            number nFolders = allFolders.TagGroupCountTags()
            for ( number i=0; i<nFolders; i++ )
            {
                string folder
                TagGroup entry
                allFolders.TagGroupgetIndexedTagAsTagGroup( i, entry )
                entry.TagGroupGetTagAsString( "Name", folder )
                folder = StringToLower(folder)
                TagGroup SubList = self.BuildList( PathConcatenate(path,folder), leadIn, leadOut, bSubfolders )
                for ( number j=0; j<SubList.TagGroupCountTags(); j++)
                {
                    string file
                    if ( SubList.tagGroupGetIndexedTagAsString(j,file))
                        FileList.TagGroupInsertTagAsString( Infinity(), file )
                }
            }
        }
        return FileList
    }
    
    TagGroup Create( object self, string root, string LLin, string LLout, number incSubFolder ){
        TagGroup fullFileList
        if ( !DoesDirectoryExist( root )  || "" == root )
        {
            root = GetApplicationDirectory( "open_save", 0 )
            if ( !GetDirectoryDialog(NULL,"Select root path", root, root ) ) 
                return fullFileList;
        }
    
        fullFileList = self.BuildList( root, LLin, LLout, incSubFolder );
        return fullFileList;
    }  
}

Class CActOnFileList{
    void ActOnFile( object self, string path ){
        // Do whatever you want with a file(path) here!
        if ( !DoesFileExist(path) ) {
            Result( "\n Not found: " + path )       // Skip with message
            // Throw( "File not found:\n" + path )  // or stop the script with error
            return
        }
        
        //Assuming it is an image, we open it 
        Image img := OpenImage(path)
        if ( !img.ImageIsValid() ) {
            Result( "\n Filt not an image: " + path )       // Skip with message
            // Throw( "File is not a valid image:\n" + path ) // or stop the script with error
            return
        }
        
        // We could show it...
        //img.ShowImage()
        
        // Instead, we convert it to 2-byte-integer and resave it with modifed name
        ConvertToShort(img)
        
        string newName = PathExtractDirectory(path,0) + "Conv_" + PathExtractBaseName(path,0) 
        img.SaveAsGatan(newName)
        
        Result("\n Converted & Saved: " + newName )
    
    }

    number PerformActionOnAllFilesInList( object self, TagGroup fileList, number bShowProgress ){
        number nFiles = fileList.TagGroupCountTags()
        for( number i = 0; i<nFiles; i++ ){
            if ( bShowProgress )
                OpenAndSetProgressWindow( "Acting on file", (i+1) + " of " +  nFiles, "" )
                
            string path
            if ( fileList.TagGroupGetIndexedTagAsString(i,path) )
                self.ActOnFile(path)
        }
        CloseProgressWindow()
    }
}

string root = ""        // Leave "" to get a prompt
string pre = ""         // Leave "" for no filter. Otherwise only paths which start with pre are kept
string suf = ".dm4"     // Leave "" for no filter. Otherwise only paths which end with suf are kept
number subfolder = 1    // Set "0" to only work on the specified folder and 1 to include all subfolders
number showProgress = 1 // Set "1" to have a progress output (status bar)


Alloc(CActOnFileList).PerformActionOnAllFilesInList( Alloc(CRecursiveFileList).Create(root, pre, suf, subfolder), showProgress )