我有一堆冷冻和液体细胞dm4图像,它们是32位浮动的,但精度和32位的大值是完全不必要的,所以我们决定将它们转换为16位有符号整数。
我需要保留dm4图像的元数据结构,因为这些图像仍然需要在数字显微照片中打开。因此,使用hyperpy或ncempy不是一个选项,因为它们不能写入dm4文件。
我目前有一个在dm脚本中执行此操作的脚本,但它一次只接收一个目录,不知道如何处理液体细胞数据。我还不够好,不能做到这一点。
我想知道我是否可以在DM中的python接口中做同样的事情?因为我可以用python轻松地操作文件结构和遍历目录。
我所指的dm脚本是FELMI ZFE DigitalMicrograph脚本数据库上的,该数据库经过轻微调整,允许使用有符号整数,而不创建tiff,因为它们目前对我们没有用处。
编辑:dm脚本目前运行得很好,我很好奇是否有办法从python脚本将源和输出目录传递给我的dm脚本。这样,我就可以在python中完成所有的目录处理,只需一次调用一个文件夹的dm脚本。
不确定这是否是您想要的,但下面的脚本显示了如何从所有子文件夹递归地构建文件列表,按扩展名进行筛选,然后对每个子文件夹执行操作。
当前脚本将打开给定文件夹及其子文件夹的所有.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 )