Function Repository Resource:

ImageRead

Source Notebook

Read and return an image file from disk

Contributed by: Arnoud Buzing

ResourceFunction["ImageRead"][type,file]

reads file as a type image and returns the image.

Details and Options

The type argument is one of the following: "JPEG", "PNG", "TIFF".
ResourceFunction["ImageRead"] returns $Failed if it cannot read the file as an image of the specified type.
ResourceFunction["ImageRead"] bypasses most of the functionality of Import, making it less flexible and more limited but significantly faster. It should be used judiciously when performance is critical.

Examples

Basic Examples (2) 

Import an image:

In[1]:=
ResourceFunction["ImageRead"]["PNG", FindFile["ExampleData/spikey2.png"]]
Out[1]=
Image

Export a random image as a PNG file:

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

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

In[3]:=
timing1 = Do[ResourceFunction["ImageRead"]["PNG", file], 100] // AbsoluteTiming // First
Out[3]=
Image

Repeat the same experiment with Import:

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

Note how ImageRead is significantly faster than Import:

In[5]:=
timing2/timing1
Out[5]=
Image

However, ImageRead only imports a file as an Image. Import provides many more options:

In[6]:=
Import[file, "Elements"]
Out[6]=
Image

Scope (2) 

Read from a "JPEG" image file:

In[7]:=
ResourceFunction["ImageRead"]["JPEG", FindFile["ExampleData/coneflower.jpg"]]
Out[7]=
Image

Read from a "TIFF" image file:

In[8]:=
ResourceFunction["ImageRead"]["TIFF", FindFile["ExampleData/spikey.tiff"]]
Out[8]=
Image

Publisher

Arnoud Buzing

Version History

  • 1.0.0 – 16 July 2020

Related Resources

License Information