首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将32位浮点型DM4s转换为带符号的16位

将32位浮点型DM4s转换为带符号的16位
EN

Stack Overflow用户
提问于 2020-08-27 20:29:32
回答 1查看 79关注 0票数 1

我有一堆32位浮点数的低温和液体细胞dm4图像,但是32位的精度和大值是完全不必要的,所以我们决定将它们转换为16位有符号整数。

我需要保留dm4图像所具有的元数据结构,因为图像仍然需要在数字微图中打开。所以不能使用hyperspy或ncempy,因为它们不能写dm4文件。

我目前在dm-script中有一个脚本可以做到这一点,但它一次只接受一个目录,并且不知道如何处理液体细胞数据。我还不够好,不能这么做。

我想知道我是否可以在DM的python接口中做同样的事情?因为我可以使用python轻松地操纵文件结构和遍历目录。

我所指的dm脚本是在FELMI ZFE DigitalMicrograph Script Database上略微调整,以允许有符号的整数,并且不创建tiffs,因为它们目前对我们没有用处。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 21:51:10

不知道这是否是你想要的,但是下面的脚本展示了如何从所有子文件夹递归地构建一个文件列表,按扩展名过滤它,然后对每个子文件夹执行操作。

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

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

代码语言:javascript
复制
// 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 )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63616036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档