# How to | Create an Image Object

Images are an important standard data structure, tightly integrated with the *Mathematica* front end and kernel. Image objects are created by default when you import any file with a supported image format. However, you can create image objects directly with some basic knowledge of the structure and properties of an image expression.

The simplest way to create an image object is to wrap Image around a matrix of real values ranging from 0 to 1:

In[17]:= |

Out[17]= |

Conveniently, the display of this tiny 2×2 image is larger than it actually is—this is an automatic feature of the front end. In creating the image *Mathematica* makes several assumptions, selecting automatic values for some properties of the image.

To verify these automatic settings, view the Options for the image:

In[27]:= |

Out[27]= |

Create a grayscale image by explicitly specifying the ColorSpace:

In[18]:= |

Out[18]= |

The ColorSpace now reflects your explicit setting:

In[11]:= |

Out[11]= |

An RGB color image is defined by a rectangular array of triples of real or integer values. Any data with such a structure can be viewed as a color image.

Use Tuples to create triplets of values corresponding to the primary and secondary colors:

In[19]:= |

Out[19]= |

Use Partition to put the list into 4 columns:

In[20]:= |

Out[20]= |

Use Image to create the color image:

In[21]:= |

Out[21]= |

Use InputForm to see the internal structure of the Image expression:

In[15]:= |

Out[15]//InputForm= | |

Use ImageData to extract the array of pixel values. By default, ImageData returns a 2D array of color values as real numbers in the range of 0 to 1:

In[16]:= |

Out[16]= |

Sometimes you may wish to view the raw image data as a list of matrices, one for each color channel. Set Interleaving to False to convert the image data to this form:

In[33]:= |

Out[33]= |

Any matrix or list of two or more matrices with the same dimensions can be used to form a new image. Sample the cosine function and use Image to create an image of a wave pattern from the data:

In[18]:= |

Out[18]= |

You can create an RGB color image by combining three such patterns, one for each of the color channels. First, generate the data by using Map to repeat the sampling calculation for a list of three different functions:

In[34]:= |

In[20]:= |

Out[20]= |

Alternatively, you can form an RGB image by combining a list of three grayscale images.

Use Map with Image to generate a grayscale image from each sampling calculation in :

In[35]:= |

Out[35]= |

Now use ColorCombine with the list of grayscale images to create the RGB image:

In[22]:= |

Out[22]= |

Two-channel images are useful in visualizing complex functions:

In[23]:= |

Out[23]= |