The machinery for Import allows custom converters to be defined for importing different file formats. The examples in the link provide details for writing such a converter:

ImportExport`RegisterImport["myFmt", {
  (*register the import of sub-elements*)
  {"Header", s_String} :> (myFmtImportHeader[s][##] &),
  {"Data", s_String} :> (myFmtImportData[s][##] &),
  (*register default*)
  myFmtDefaultImporter}, {}]

myFmtDefaultImporter[file_String, opts___?OptionQ] := 
 Module[{data, header},
  header = fnHeader[file, opts];
  data = fnData[file, opts];
  {"Header" -> header, "Data" -> data}]

Here, the sub-elements are hooked in as sub-values:

myFmtImportHeader[s_String][file_String, opts___?OptionQ] :=
  Module[{header}, header = myFmtReadHeader[file, opts];
         "Header" -> header]

My question is, what should the syntax look like for more complicated functions that take parameters? For instance, the built-in TIFF routines can handle various specifications for elements and parameters, such as:

Import["ExampleData/lena.tif", {"TIFF", "Thumbnail", Tiny}]
Import["ExampleData/lena.tif", {"TIFF", "Thumbnail", {1,1}}]
Import["ExampleData/lena.tif", {"TIFF", "ThumbnailList", 1, Tiny}]

If I have an import function:

fnReadImages[file_, frames_, opts___]

How can I register it so I can call it with something like:

Import[file, {"myFmt", "Images", frames}]

where frames is an integer, list of integers, or a keyword passed to the function?


1 Answer 1


RegisterImport has specific requirements for the form of the output returned by conditional raw importers:

The output of the low-level function must match the form {elem->{subelem->output}}.

The following syntax seems to work for specifying a single parameter:

ImportExport`RegisterImport["myFmt", {"Header", s_String} :> (myFmtImportHeader[s][##] &),
                            myFmtDefaultImporter}, {}]

myFmtImportHeader[s_String][file_String, opts___?OptionQ] :=
     "val1", header = {dummy1, s},
     "val1", header = {dummy2, s},
     _, header = "---ERROR---"];
   {"Header" -> {s -> header}}]

Import[file, {"myFmt", "Header", "val1"}]
(* {dummy1, val1} *)

So far I haven't been able to extend this to use more than one parameter, as in the third Import TIFF example in the question text.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.