How to | Import and Export Images
Of the many ways images can be loaded into a
Mathematica notebook,
Import remains the standard method to access image files on your local computer or 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 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:
| 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:
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:
| Out[3]= |  |
Return the size of the image:
| Out[4]= |  |
| 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:
| Out[6]= |  |
Permute the channels in six different ways and form color images from each:
| 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:
Use
FileNameJoin to construct a file name using a simple indexing scheme:
| Out[9]= |  |
Use
Table with
Export to iterate the naming of the files based on the indexing scheme:
| Out[10]= |  |
Import one of the saved images to verify the save operation:
| Out[11]= |  |