1
$\begingroup$

I'm a relatively new user of Mathematica, and currently using it for image processing of multiple tiff movies. I am also exporting tiff files, and I would like a way of naming my exported files after the corresponding input files.

For example I have a folder that contains two movies called Movie1.tiff and Movie2.tiff, I start with this code:

raw = FileNames["*.tif", SystemDialogInput["Directory"]];

movies = Import[#] & /@ raw;

I have created function f that processes the movies, so in order to export I use the following code, having already specified the directory 'dir':

Do[Export[
     dir <> DateString["Time"] <> "timepoint" <> ToString[i] <> "_" <> 
      ".tif", f[#[[i]]]], {i, 1, Length[#]}] & /@ movies;

The reason for including DateString is because otherwise, the first movie file exports "timepoint1.tiff, timepoint2.tiff, timepoint3.tiff..." etc, but then the second movie exports with the same filenames so it overwrites.

I would be very grateful if someone could suggest a better way of naming the two sets of exported tiff files, preferentially including the filename of the imported files, for example: "Movie1_timepoint1.tiff, "Movie1_timepoint2.tiff..." etc.

Apologies for the long question! Thank you very much in advance!

$\endgroup$
  • 2
    $\begingroup$ You can use the information that is stored raw. $\endgroup$ – Karsten 7. Dec 8 '15 at 14:02
1
$\begingroup$

you'll need to add the information of the filenames to the Export command, because your movies contains only the video data but no filenames. The filenames are still contain as String in your raw. To improve your code in a simple way I would create a matrix where the data of movies and raw are aligned e.g. Transpose[{movies,raw}]

Then your code should be changed to

    Do[Export[
     dir <> #[[i, 2]] <> DateString["Time"] <> "timepoint" <> ToString[i] <>"_" <> .tif",
     f[#[[i,1]]]], {i, 1, Length[#]}] & /@ Transpose[{movies,raw}];

With #[[i, 2]] accessing the second row (and thus the names of the videos) and with #[[i,1]] accesing the video data.

One more tipp. If the length of the images is bigger than 9 the way you are trying to work will provide you with a not so nice structure in e.g. the Windows explorer as (short name) filename1.dat will be followed first by filename10.dat and then only by filename2.dat

To prevent this from happening you can use the following trick: add a large enough number (larger than the number of elements you want to export) to your i, e.g. 1000+i convert this to String and remove the first character: StringDrop[ToString[1000+i],1] then your file names will be named as

001

002

...

009

010

and so on. In your code this would be then

Do[Export[
     dir <> #[[i, 2]] <> DateString["Time"] <> "timepoint" <> 
      StringDrop[ToString[1000+i],1] <>"_" <> .tif",
     f[#[[i,1]]]], {i, 1, Length[#]}] & /@ Transpose[{movies,raw}];

Hope this helps

$\endgroup$
  • 1
    $\begingroup$ for StringDrop[ToString[1000+i],1], try IntegerString[i, 10, 3] $\endgroup$ – dwa May 3 '17 at 6:15
  • $\begingroup$ @dwa: thanks I wasn`t aware of that command $\endgroup$ – Quit007 Nov 7 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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