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:
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:
Create a grayscale image by explicitly specifying the ColorSpace:
The ColorSpace now reflects your explicit setting:
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:
Use Partition to put the list into 4 columns:
Use Image to create the color image:
Use InputForm to see the internal structure of the Image expression:
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:
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:
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:
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:
Now create the image:
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 :
Now use ColorCombine with the list of grayscale images to create the RGB image:
Two-channel images are useful in visualizing complex functions: