How to | Create an Image Object

Images are an important standard data structure, tightly integrated with the Wolfram System 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[1]:=
Click for copyable input
Out[1]=

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

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

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

Create a grayscale image by explicitly specifying the ColorSpace:

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

The ColorSpace now reflects your explicit setting:

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

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[5]:=
Click for copyable input
Out[5]=

Use Partition to put the list into 4 columns:

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

Use Image to create the color image:

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

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

In[8]:=
Click for copyable input
Out[8]//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[9]:=
Click for copyable input
Out[9]=

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[10]:=
Click for copyable input
Out[10]=
    

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[1]:=
Click for copyable input
Out[1]=

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[2]:=
Click for copyable input

Now create the image:

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

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[4]:=
Click for copyable input
Out[4]=

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

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

Two-channel images are useful in visualizing complex functions:

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