简体   繁体   English

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

[英]Converting 32bit float DM4s to signed 16bit

I have a bunch of cryo and liquid cell dm4 images that are 32bit float, but the precision and the large values of 32bit are completely unnecessary, so we've decided to convert them to 16bit signed int.我有一堆 32 位浮点数的冷冻和液体细胞 dm4 图像,但是 32 位的精度和大值是完全没有必要的,所以我们决定将它们转换为 16 位有符号整数。

I need to preserve the metadata structure that dm4 images have because the images still need to be openable in digital micrograph.我需要保留 dm4 图像具有的元数据结构,因为图像仍然需要在数字显微照片中可打开。 So using hyperspy or ncempy is not an option since they can't write dm4 files.因此,使用 hyperspy 或 ncempy 不是一种选择,因为它们无法编写 dm4 文件。

I currently have a script to do this in dm-script, but it only takes in one directory at a time and doesn't know how to handle liquid cell data.我目前有一个在 dm-script 中执行此操作的脚本,但它一次只接受一个目录,并且不知道如何处理液体细胞数据。 And I am not good enough to do that.而我还不够好,无法做到这一点。

I was wondering if I could do the same thing in the python interface in DM?我想知道我是否可以在 DM 的 python 界面中做同样的事情? Since I can manipulate the file structure and traverse the directories with easily with python.因为我可以使用python轻松操作文件结构并遍历目录。

The dm script I'm referring to is the on FELMI ZFE DigitalMicrograph Script Database slightly adjusted to allow for signed integers and not creating tiffs since they are not useful for us currently.我所指的 dm 脚本是FELMI ZFE DigitalMicrograph 脚本数据库上的稍微调整以允许有符号整数并且不创建 tiff,因为它们目前对我们没有用。

Edit: the dm-script works nicely for now, i am curious if there is a way to pass the source and output directory to my dm-script from the python script.编辑:dm-script 目前运行良好,我很好奇是否有办法将源和输出目录从 python 脚本传递到我的 dm-script。 That way i can do all the directory handling in python, and just call the dm-script one folder at a time.这样我就可以在 python 中完成所有的目录处理,并且一次只调用一个文件夹 dm-script。

Not sure if this is what you're after, but the script below shows how one recursively builds a file-list from all subfolders, filters it by extension, and then act on each of them.不确定这是否是您所追求的,但下面的脚本显示了如何从所有子文件夹递归构建文件列表,按扩展名过滤它,然后对每个子文件夹采取行动。

The current script will open all .dm4 files of a given folder and its subfolders, convert the image into sint16 and resave it with a prefixed name.当前脚本将打开给定文件夹及其子文件夹的所有 .dm4 文件,将图像转换为 sint16 并使用前缀名称重新保存。

However, you can very easily adjust the script by modifying the method ActOnFile .但是,您可以通过修改方法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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM