How to automate generation of image files?
To export a sequence of plots, you don't need to do the sequential numbering by hand (or by using ToString
). Instead, there is the Export
option "VideoFrames"
that does the numbering automatically:
plots =
Table[Plot[Sin[x + a], {x, 0, Pi},
PlotRange -> {{0, Pi}, {-1.1, 1.1}}], {a, 0, Pi, .1}];
Quiet[CreateDirectory["output"]];
SetDirectory["output"];
Export["plot001.png", plots, "VideoFrames"];
FileNames[]
(*
==> {"plot001.png", "plot002.png", "plot003.png", "plot004.png", \
"plot005.png", "plot006.png", "plot007.png", "plot008.png", \
"plot009.png", "plot010.png", "plot011.png", "plot012.png", \
"plot013.png", "plot014.png", "plot015.png", "plot016.png", \
"plot017.png", "plot018.png", "plot019.png", "plot020.png", \
"plot021.png", "plot022.png", "plot023.png", "plot024.png", \
"plot025.png", "plot026.png", "plot027.png", "plot028.png", \
"plot029.png", "plot030.png", "plot031.png", "plot032.png"}
*)
ResetDirectory[];
The number of digits in the numbered output files is determined from the number you specify in the first file name.
Let's suppose that these are your imported data (three sets of random numbers):
importedData = Table[RandomReal[{0, 1}, {10, 2}], {3}];
Now we can directly produce and export the PNG files:
Table[
Export[
"plot_" <> ToString[i] <> ".png",
ListPlot[importedData[[i]]],
"PNG"
],
{i, Length[importedData]}
]
{"plot_1.png", "plot_2.png", "plot_3.png"}
"plot_" <> ToString[i] <> ".png"
can be replaced by StringTemplate["plot_``.png"] @i