微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将32位浮点DM4转换为带符号的16位

我有一堆32位浮点的冷冻和液槽dm4图像,但是完全不需要32bit的精度和较大的值,因此我们决定将它们转换为16bit带符号的int。

我需要保留dm4图像的元数据结构,因为这些图像仍需要在数字显微照片中可打开。因此,不能使用hyperspy或ncempy,因为它们无法写入dm4文件

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

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

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

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

解决方法

不确定这是您要执行的操作,但是下面的脚本显示了一个如何从所有子文件夹递归构建文件列表,并按扩展名对其进行过滤,然后对每个子文件夹执行操作。

当前脚本将打开给定文件夹及其子文件夹的所有.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 )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。