DM Script to import a 2D image in text (CSV) format

纵然是瞬间 提交于 2021-01-28 09:20:42

问题


Using the built-in "Import Data..." functionality we can import a properly formatted text file (like CSV and/or tab-delimited) as an image. It is rather straight forward to write a script to do so. However, my scripting approach is not efficient - which requires me to loop through each raw (use the "StreamReadTextLine" function) so it takes a while to get a 512x512 image imported.

Is there a better way or an "undocumented" script function that I can tap in?


回答1:


DigitalMicrograph offers an import functionality via the File/Import Data... menu entry, which will give you this dialog:

The functionality evoked by this dialog can also be accessed by script commands, with the command

BasicImage ImageImportTextData( String img_name, ScriptObject stream, Number data_type_enum, ScriptObject img_size, Boolean lines_are_rows, Boolean size_by_counting )

As with the dialog, one has to pre-specify a few things.

The data type of the image.

This is a number. You can find out which number belongs to which image data type by, f.e., creating an image outputting its data type:

image img := Realimage( "", 4, 100 )
Result("\n" + img.ImageGetDataType() )

The file stream object

This object describes where the data is stored. The F1 help-documention explains how one creates a file-stream from an existing file, but essentially you need to specify a path to the file, then open the file for reading (which gives you a handle), and then using the fileHandle to create the stream object.

string path = "C:\\test.txt"
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )

The image size object

This is a specific script object you need to allocate. It wraps image size information. In case of auto-detecting the size from the text, you don't need to specify the actual size, but you still need the object.

object imgSizeObj = Alloc("ImageData_ImageDataSize")
imgSizeObj.SetNumDimensions(2)      // Not needed for counting!
imgSizeObj.SetDimensionSize(0,10)   // Not used for counting
imgSizeObj.SetDimensionSize(1,10)   // Not used for counting

Boolean checks

Like with the checkboxes in the UI, you spefic two conditions:

  • Lines are Rows
  • Get Size By Counting

Note, that the "counting" flag is only used if "Lines are Rows" is also true. Same as with the dialog.


The following script improrts a text file with couting:

image ImportTextByCounting( string path, number DataType )
{

    number fRef     = OpenFileForReading( path )
    object fStream  = NewStreamFromFileReference( fRef, 1 )
    number bLinesAreRows    = 1
    number bSizeByCount     = 1 
    bSizeByCount *= bLinesAreRows // Only valid together!

    object imgSizeObj = Alloc("ImageData_ImageDataSize")

    image img := ImageImportTextData( "Imag Name ", fStream, DataType, imgSizeObj, bLinesAreRows, bSizeByCount )
    return img
}

string path = "C:\\test.txt"
number kREAL4_DATA      = 2
image img := ImportTextByCounting( path, kREAL4_DATA )
img.ShowImage()


来源:https://stackoverflow.com/questions/48958038/dm-script-to-import-a-2d-image-in-text-csv-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!