2
$\begingroup$

For importing png images I use often:

image = First@Image`ImportExportDump`ImageReadPNG[fileName]
(see: ?*`*PNG*)

since this is faster than:

image = Import[fileName,"PNG"]

(see for comparison here, the answer of MarcoB)

But the problem is: When the filename is varying (different images names) and I want to read in many png images with Image`ImportExportDump`ImageReadPNG[fileName] then the memory consumption increases endlessly.

Finally at a certain point (depending on image size and available memory) the execution of the notebook stops without any error or warning.

What could be the reason for that? Please run the code below and observe how the used memory increases.

This "problem/error" does not occur with Import[filename,"PNG"].

Example code:

SetDirectory[$TemporaryDirectory];

numberImages = 20000;

Do[

  imageData = RandomReal[{0, 1}, {2000, 2000}];

  fileName = StringJoin[RandomChoice[{"a", "b", "c"}, 20], ".png"];
  fullFileName = StringJoin[$TemporaryDirectory, "\\", fileName];

  image = Image[imageData];

  Export[fullFileName, image, "PNG"];

  image = First@Image`ImportExportDump`ImageReadPNG[fullFileName];

  If[Mod[i, 100] == 0, Print[i]];

  , {i, 1, numberImages}

  ];

Im am running Mathematica 11.3 on Windows 10 Pro 64bit.

$\endgroup$
2
$\begingroup$

Image`ImportExportDump`ImageReadPNG[fileName] uses a cache. You should call Image`ImportExportDump`DeleteCachePNG[]. To improve the performance of Import you can use Import["file.png", IncludeMetaInformation->None].

$\endgroup$
  • $\begingroup$ Tanks a lot for the solution Piotr. $\endgroup$ – mrz Dec 12 '18 at 19:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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