This question already has an answer here:

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 ?


marked as duplicate by rhermans, Henrik Schumacher, m_goldberg, MarcoB, Hector Aug 9 '18 at 14:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @rhermans well, simply be increasing number : 1,2,3,4,5, etc. $\endgroup$ – james Aug 8 '18 at 9:31
  • 1
    $\begingroup$ 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. $\endgroup$ – bill s Aug 8 '18 at 15:46

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

   , {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

 , {k, Length[FileNames["*.jpg"]]}

Sort FileNames

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

Or, adapted from here

Import /@  With[
 {fnl = FileNames["*.jpg"]},
    StringSplit[fnl, x : NumberString :> ToExpression@x][[All, 1]]]]]
  • $\begingroup$ Thank you very much ! $\endgroup$ – james Aug 8 '18 at 9:42

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