How to | Import and Export Images

Of the many ways images can be loaded into a Mathematica notebook, Import is the main method used to access image files on your local computer or at a remote location. The import and export of images are the most common first and last steps of practically any image processing computation.

Use the Insert ► File Path menu item or type a file path to import a local image file as an image object.

This example imports an image from the Mathematica documentation directory ExampleData:

In[1]:=
Click for copyable input
Out[1]=

Mathematica provides a general mechanism for naming and finding files that works on all systems.

Use FileNameJoin to construct a complete path to a file on the local system:

In[2]:=
Click for copyable input

By specifying the elements of an image for Import, you can get useful information before loading it into the kernel. The file type is automatically deduced from the file name extension:

In[3]:=
Click for copyable input
Out[3]=

Return the size of the image:

In[4]:=
Click for copyable input
Out[4]=

Now import the image:

In[5]:=
Click for copyable input
Out[5]=

Once you have imported the image, you can modify and analyze it. As a simple example, consider how the image changes appearance under different permutations of its color channels.

Separate the color channels:

In[6]:=
Click for copyable input
Out[6]=

Permute the channels in six different ways and form color images from each:

In[7]:=
Click for copyable input
Out[7]=

You can now save the six different images in your local file system by exporting them in any of the many supported file formats. In this example, the JPEG format is used because it stores efficiently.

You can export to an existing directory or create a new directory from within Mathematica. Here, CreateDirectory is used to create a directory in the local system's temporary storage area:

In[8]:=
Click for copyable input

Use FileNameJoin to construct a file name using a simple indexing scheme:

In[10]:=
Click for copyable input
Out[10]=

Use Table with Export to iterate the naming of the files based on the indexing scheme:

In[12]:=
Click for copyable input
Out[12]=

Import one of the saved images to verify the save operation:

In[13]:=
Click for copyable input
Out[13]=
New to Mathematica? Find your learning path »
Have a question? Ask support »