Function Resource

ReadPNG

Reads a PNG file from disk and returns the image

ResourceFunction["ReadPNG"][file]

reads file as a PNG image and returns the image.

Details and Options

ResourceFunction["ReadPNG"] returns $Failed if it can not read the file as a "PNG" image.
ResourceFunction["ReadPNG"] bypasses most of the functionality of the Import making it less flexible and more limited, but significantly faster. It should be used judiciously when performance is critical.

Examples

Basic Examples

Export a random image as a PNG file:

In[1]:=
file = Export["test.png", RandomImage[1, {1000, 1000}]]
Out[1]=

Import the file with ReadPNG one hundred times, noting the elapsed time:

In[2]:=
timing1 = Do[ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/ReadPNG"][file], 100] // AbsoluteTiming // First
Out[2]=

Repeat the same experiment with Import:

In[3]:=
timing2 = Do[Import[file], 100] // AbsoluteTiming // First
Out[3]=

Note how ReadPNG is significantly faster than Import:

In[4]:=
timing2/timing1
Out[4]=

ReadPNG only imports a file as an Image. Import provides many more options:

In[5]:=
Import[file, "Elements"]
Out[5]=