1

I would like to import images which are frames from a video, they are numbered, so importing them in the correct order should not be a problem...

enter image description here

However, when I import them, by writing:

SetDirectory[NotebookDirectory[] <> "Videos\\" <> ToString[1]];
framest = Import[#] & /@ FileNames["*.jpg"];
Print["Got all frames!"];

The order is random:

enter image description here

How can I resolve that ?

rhermans
  • 36,518
  • 4
  • 57
  • 149
james
  • 3,043
  • 13
  • 29
  • @rhermans well, simply be increasing number : 1,2,3,4,5, etc. – james Aug 08 '18 at 09:31
  • 1
    Perhaps the simplest solution is to have whatever software you are using to break the video into individual pictures use an unambiguous naming strategy like image-01.jpg, image-02.jpg etc. – bill s Aug 08 '18 at 15:46

1 Answers1

3

The problem, very likely, is that the files are not random but sorted by string name. For example:

Sort@Table[
   StringTemplate["``.jpg"]@k
   , {k, 30}
   ]
(* {"10.jpg", "11.jpg", "12.jpg", "13.jpg", "14.jpg", "15.jpg", \
"16.jpg", "17.jpg", "18.jpg", "19.jpg", "1.jpg", "20.jpg", "21.jpg", \
"22.jpg", "23.jpg", "24.jpg", "25.jpg", "26.jpg", "27.jpg", "28.jpg", \
"29.jpg", "2.jpg", "30.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", \
"7.jpg", "8.jpg", "9.jpg"} *)

So you need to generate a file list in order, here some options.

Create list

Table[
 Import[StringTemplate["``.jpg"]@k]
 , {k, Length[FileNames["*.jpg"]]}
 ]

Sort FileNames

Import /@ SortBy[FileNames["*.jpg"], ToExpression@StringDelete[#, ".jpg"] &]

Or, adapted from here

Import /@  With[
 {fnl = FileNames["*.jpg"]},
 fnl[[Ordering[
    StringSplit[fnl, x : NumberString :> ToExpression@x][[All, 1]]]]]
 ]
rhermans
  • 36,518
  • 4
  • 57
  • 149