3
Table[
  Export[
    StringJoin["x", ToString[i], ".png"],
    Style[StringJoin["x", ToString[i]], RGBColor["#FF00FF"], 1024]
  ],
  {i, 1, 200}
]

How to make it faster?

MarcoB
  • 67,153
  • 18
  • 91
  • 189

2 Answers2

7

For example, you could export as svg and use a dedicated tool such as ffmpeg for rasterization. svg export is 20 times faster and ffmpeg is really fast for batch processing tasks (I used it quite often for creating videos from sequences of images).

ParallelDo[
 Export[
  StringJoin["x", IntegerString[i, 10, 4], ".svg"],
  Style[StringJoin["x", IntegerString[i, 10]], RGBColor["#FF00FF"], 
   1024]
  ], {i, 1, 200}] // AbsoluteTiming // First

2.34546

Then run

ffmpeg -width 1024 -y -i x%04d.svg x%04d.png

in the command line. This does the job within a few seconds. You will need a rather recent version of ffmpeg for it (3.4 or higher).

You can run the command line code also from within Mathematica with

ffmpegpath = "/opt/local/bin/";
filebasename = "x";
s = StringJoin["! ", FileNameJoin[{ffmpegpath, "ffmpeg"}], 
  " -width 1024 -y -i ", filebasename, "%04d.svg ", filebasename, 
  "%04d.png"]
Import[s, "Text"] // AbsoluteTiming

"! /opt/local/bin/ffmpeg -width 1024 -y -i x%04d.svg x%04d.png"

12.4168

Unfortunately, the svg rasterization is currently not as well parallelized as other task in ffmpeg...

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
0

This approach uses ImageAssemble.

Firstly, export individual element images containing x, 1, 2, and so on. Secondly, import those individual images and store it using an Association. Finally, assemble the images according to the specified text and export them.

path = "D:\\Desktop\\stack";
Export[path <> "\\elementImages\\x.png", Style[x, RGBColor["#FF00FF"], 1024]];
Table[Export[path <> "\\elementImages\\" <> ToString[i] <> ".png", 
   Style[ToString[i], RGBColor["#FF00FF"], 1024]], {i, 0, 9}];

xImage = Import[path <> "\\elementImages\\x.png"];
digitImage = 
  Table[i -> Import[path <> "\\elementImages\\" <> ToString[i] <> ".png"], {i, 0, 9}]
 // Association;

exportImageAssembled[i_] := 
  Export[path <> "\\x" <> ToString@i <> ".png", 
   ImageAssemble[{xImage, Sequence @@ (digitImage /@ IntegerDigits[i])}]];

For a fair comparison, I use the parallelized version of OP's code.

ParallelDo[exportImageAssembled[i], {i, 200}]; // AbsoluteTiming//First

ParallelDo[
    Export[StringJoin["D:\\Desktop\\stack\\OP\\x", ToString[i], ".png"], 
     Style[StringJoin["x", ToString[i]], RGBColor["#FF00FF"], 1024]], 
      {i, 1, 200}]; // AbsoluteTiming // First

8.09534

89.6499

ImageData comparison of an exported image from both of these approaches.

a1 = ImageData[ Import@Export[ StringJoin[path <> "\\OP\\x", ToString[117], ".png"], 
     Style[StringJoin["x", ToString[117]], RGBColor["#FF00FF"], 1024]]];
a2 = ImageData[Import@exportImageAssembled[117]];
a1 == a2

True

Anjan Kumar
  • 4,979
  • 1
  • 15
  • 28