---
title: "LinearGradientImage"
language: "en"
type: "Symbol"
summary: "LinearGradientImage[gcol] returns an image with values linearly changing from left to right based on gradient color gcol. LinearGradientImage[{pos1, pos2} -> gcol] returns an image where the gradient starts at pos1 and ends at pos2. LinearGradientImage[..., size] returns a linear gradient image of the specified size. LinearGradientImage[..., size, type] gives an image converted to the specified type."
keywords: 
- gradient image
- color shades
- linear gradient image
- vertical gradient image
- horizontal gradient image
- diagonal gradient image
- color gradient
- linear color gradient
canonical_url: "https://reference.wolfram.com/language/ref/LinearGradientImage.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Creating & Importing Images"
    link: "https://reference.wolfram.com/language/guide/CreatingAndImportingImages.en.md"
related_functions: 
  - 
    title: "RadialGradientImage"
    link: "https://reference.wolfram.com/language/ref/RadialGradientImage.en.md"
  - 
    title: "Blend"
    link: "https://reference.wolfram.com/language/ref/Blend.en.md"
  - 
    title: "ConstantImage"
    link: "https://reference.wolfram.com/language/ref/ConstantImage.en.md"
  - 
    title: "RandomImage"
    link: "https://reference.wolfram.com/language/ref/RandomImage.en.md"
  - 
    title: "Image"
    link: "https://reference.wolfram.com/language/ref/Image.en.md"
  - 
    title: "ColorData"
    link: "https://reference.wolfram.com/language/ref/ColorData.en.md"
  - 
    title: "GradientFilter"
    link: "https://reference.wolfram.com/language/ref/GradientFilter.en.md"
---
# LinearGradientImage

LinearGradientImage[gcol] returns an image with values linearly changing from left to right based on gradient color gcol.

LinearGradientImage[{pos1, pos2} -> gcol] returns an image where the gradient starts at pos1 and ends at pos2.

LinearGradientImage[…, size] returns a linear gradient image of the specified size.

LinearGradientImage[…, size, "type"] gives an image converted to the specified type.

## Details and Options

* ``LinearGradientImage`` constructs a 2D or 3D image with constant gradient along a direction derived from the input specification.

* Color scheme specification ``gcol`` can be any of the following:

|                 |                                                            |
| --------------- | ---------------------------------------------------------- |
| {col1, col2, …} | blend of multiple color directives coli                    |
| "name"          | ColorData["name"]                                          |
| f               | function f that returns a color based on a scalar distance |

* The argument passed to the function ``f`` is by default the normalized distance between a pixel and the line going through ``pos1``, perpendicular to ``pos2 - pos1``.

* Position specification ``posi`` can be any of the following:

|                     |                                                                                            |
| ------------------- | ------------------------------------------------------------------------------------------ |
| {x, y} or {x, y, z} | absolute pixel position                                                                    |
| Left, Right         | $x$ axis in 2D and 3D                                        |
| Bottom, Top         | $y$ axis in 2D, $z$ axis in 3D |
| Front, Back         | $y$ axis in 3D                                               |
| Center              | center alignment                                                                           |
| {posx, …}           | a list of named positions                                                                  |

* Positions that are not constrained are taken to be centered.

* By default, positions are assumed to be in the standard image coordinate system.

[image]

* The ``size`` specification can be one of the following:

|                        |                               |
| ---------------------- | ----------------------------- |
| side                   | 2D image of size {side, side} |
| {width, height}        | 2D image size specification   |
| {width, depth, height} | 3D image size specification   |

* The default ``size`` is ``{150, 150}`` for 2D images and ``{64, 64, 64}`` for 3D images.

* ``LinearGradientImage[]`` is equivalent to ``LinearGradientImage[{Black, White}]``.

* ``LinearGradientImage`` accepts all ``Image`` and ``Image3D`` options with the following additions and changes:

|                       |           |                                                |
| --------------------- | --------- | ---------------------------------------------- |
| ColorFunction         | Automatic | how each pixel should be colored               |
| ColorFunctionScaling  | True      | whether to scale the argument to ColorFunction |
| DataRange             | Full      | range of coordinates in the original image     |
| Padding               | "Fixed"   | padding scheme                                 |

* Possible settings for ``DataRange`` include:

|                                               |                                                                    |
| --------------------------------------------- | ------------------------------------------------------------------ |
| Automatic                                     | {{0, 1}, {0, h / w}} in 2D, {{0, 1}, {0, d / w}, {0, h / w}} in 3D |
| Full                                          | {{0, w}, {0, h}} in 2D, {{0, w}, {0, d}, {0, h}} in 3D (default)   |
| {{left, right}, {bottom, top}}                | explicit coordinate ranges in 2D                                   |
| {{left, right}, {front, back}, {bottom, top}} | explicit coordinate ranges in 3D                                   |

## Examples (28)

### Basic Examples (4)

Linear grayscale gradient from left to right:

```wl
In[1]:= LinearGradientImage[]

Out[1]= [image]
```

---

Linear blend of colors from left to right:

```wl
In[1]:= LinearGradientImage[{Blue, Yellow, Red}]

Out[1]= [image]
```

---

Linear gradient image along a given direction:

```wl
In[1]:= LinearGradientImage[{{Left, Bottom}, {Right, Top}} -> {Blue, Yellow, Red}]

Out[1]= [image]
```

---

Specify the output size:

```wl
In[1]:= LinearGradientImage[{Blue, Yellow, Red}, {150, 50}]

Out[1]= [image]
```

### Scope (14)

#### Color Schemes (5)

Linear blend of two colors:

```wl
In[1]:= LinearGradientImage[{Red, Yellow}]

Out[1]= [image]
```

---

Linear blend of multiple colors:

```wl
In[1]:= LinearGradientImage[{Red, Blue, Green, Yellow}]

Out[1]= [image]
```

---

Use a gradient color scheme:

```wl
In[1]:= LinearGradientImage["Rainbow"]

Out[1]= [image]
```

---

Use hue as the coloring function:

```wl
In[1]:= LinearGradientImage[Hue]

Out[1]= [image]
```

---

Specify a custom color function:

```wl
In[1]:= LinearGradientImage[RGBColor[#, 1 - #, # / 2]&]

Out[1]= [image]
```

#### Positions (6)

By default, a linear gradient image from left to right is generated:

```wl
In[1]:= LinearGradientImage[ColorData["Rainbow"]]

Out[1]= [image]
```

---

A top-to-bottom gradient image:

```wl
In[1]:= LinearGradientImage[{Top, Bottom} -> ColorData["Rainbow"]]

Out[1]= [image]
```

---

A diagonal gradient image:

```wl
In[1]:= LinearGradientImage[{{Left, Bottom}, {Right, Top}} -> ColorData["Rainbow"]]

Out[1]= [image]
```

---

If position along one dimension is not given, center alignment is used for that dimension:

```wl
In[1]:= LinearGradientImage[{Left, Top} -> ColorData["Rainbow"]]

Out[1]= [image]
```

---

Left-to-right 3D gradient image:

```wl
In[1]:= LinearGradientImage[{Left, Right} -> ColorData["Rainbow"], {7, 7, 7}]

Out[1]= [image]
```

---

Diagonal 3D gradient image:

```wl
In[1]:= LinearGradientImage[{{Left, Front, Top}, {Right, Back, Bottom}} -> ColorData["Rainbow"], {8, 5, 3}]

Out[1]= [image]
```

#### Size (3)

By default, a 2D image of size $\{150,150\}$ is generated:

```wl
In[1]:= LinearGradientImage[Hue]

Out[1]= [image]
```

---

If absolute pixel positions are given, the minimum image dimensions containing the positions are returned:

```wl
In[1]:= LinearGradientImage[{{0, 0}, {100, 100}} -> Hue]

Out[1]= [image]
```

Specify a different output size:

```wl
In[2]:= LinearGradientImage[{{0, 0}, {100, 100}} -> Hue, {150, 150}]

Out[2]= [image]
```

---

By default, a 3D image of size $\{64,64,64\}$ is generated:

```wl
In[1]:= LinearGradientImage[{{Left, Front, Bottom}, {Right, Back, Top}} -> "Rainbow"]

Out[1]= [image]
```

Specify the output size:

```wl
In[2]:= LinearGradientImage[{{Left, Front, Bottom}, {Right, Back, Top}} -> ColorData["Rainbow"], {40, 30, 20}]

Out[2]= [image]
```

### Options (8)

#### ColorFunction (3)

With no color specification, ``GrayLevel`` is the default color function:

```wl
In[1]:= LinearGradientImage[{{0, 0}, {100, 100}}, {100, 100}]

Out[1]= [image]
```

Specify a different color function:

```wl
In[2]:= LinearGradientImage[{{0, 0}, {100, 100}}, {100, 100}, ColorFunction -> Hue]

Out[2]= [image]
```

Use a built-in color gradient:

```wl
In[3]:= LinearGradientImage[{{0, 0}, {100, 100}}, {100, 100}, ColorFunction -> ColorData["Rainbow"]]

Out[3]= [image]
```

Use ``Blend`` to specify a color function that is a blend of multiple colors:

```wl
In[4]:= LinearGradientImage[{{0, 0}, {100, 100}}, {100, 100}, ColorFunction -> (Blend[{Blue, Yellow, Green}, #]&)]

Out[4]= [image]
```

---

When grayscale 3D images are produced, ``ColorFunction -> "GrayLevelOpacity"`` is used in ``Image3D`` :

```wl
In[1]:= LinearGradientImage[{Left, Right}, {10, 10, 10}]

Out[1]= [image]
```

---

With both ``ColorFunction`` and end point colors specified, color function is ignored:

```wl
In[1]:= LinearGradientImage[{Left -> Green, Right -> Red}, ColorFunction -> GrayLevel]

Out[1]= [image]
```

#### ColorFunctionScaling (2)

By default, the color function parameters are rescaled between 0 and 1:

```wl
In[1]:= LinearGradientImage[(Blend[{{0, Red}, {1, Yellow}}, #]&), {200, 20}]

Out[1]= [image]
```

---

Scaled values might not be suitable for some color functions:

```wl
In[1]:= Image[LinearGradientImage[ColorData[2], {20, 5}], ImageSize -> 100]

Out[1]= [image]
```

Use ``ColorFunctionScaling -> False`` :

```wl
In[2]:= Image[LinearGradientImage[ColorData[2], {20, 5}, ColorFunctionScaling -> False], ImageSize -> 100]

Out[2]= [image]
```

#### DataRange (2)

The default ``DataRange`` is the size of the image:

```wl
In[1]:= LinearGradientImage[{{0, 0}, {50, 50}} -> Hue, {100, 100}]

Out[1]= [image]
```

Specify a different data range:

```wl
In[2]:= LinearGradientImage[{{0, 0}, {50, 50}} -> Hue, {100, 100}, DataRange -> {{0, 50}, {0, 50}}]

Out[2]= [image]
```

---

Use a normalized data range to get the same effect, regardless of the image dimensions:

```wl
In[1]:= LinearGradientImage[{{1 / 2, 1 / 2}, {1, 1}} -> Hue, {50, 50}, DataRange -> {{0, 1}, {0, 1}}]

Out[1]= [image]

In[2]:= LinearGradientImage[{{1 / 2, 1 / 2}, {1, 1}} -> Hue, {100, 50}, DataRange -> {{0, 1}, {0, 1}}]

Out[2]= [image]
```

#### Padding (1)

By default, ``"Fixed"`` padding is used:

```wl
In[1]:= LinearGradientImage[{{20, 20}, {80, 80}}, {100, 100}]

Out[1]= [image]
```

Use a constant padding value:

```wl
In[2]:= LinearGradientImage[{{20, 20}, {80, 80}}, {100, 100}, Padding -> Red]

Out[2]= [image]
```

Specify a padding scheme:

```wl
In[3]:= LinearGradientImage[{{20, 20}, {80, 80}}, {100, 100}, Padding -> "Periodic"]

Out[3]= [image]
```

### Applications (2)

Apply Bayer's ``4×4`` dispersed-dot dither matrix to a gradient image:

```wl
In[1]:= B4 = {{8, 136, 40, 168}, {200, 72, 232, 104}, {56, 184, 24, 152}, {248, 120, 216, 88}} / 255

Out[1]= {{(8/255), (8/15), (8/51), (56/85)}, {(40/51), (24/85), (232/255), (104/255)}, {(56/255), (184/255), (8/85), (152/255)}, {(248/255), (8/17), (72/85), (88/255)}}

In[2]:= i = LinearGradientImage[GrayLevel, {250, 50}]

Out[2]= [image]

In[3]:= Binarize[i + Image[N@PadRight[B4, {50, 250}, B4]], 1]

Out[3]= [image]
```

---

Apply different gradient effects to an image:

```wl
In[1]:=
img = [image];
{g1, g2} = LinearGradientImage[{Bottom, {Center, 1 / 3}} -> #, ImageDimensions[img], DataRange -> {0, 1}]& /@ {"SandyTerrain", "GreenBrownTerrain"}

Out[1]= {[image], [image]}

In[2]:= Blend[{img, #}]& /@ {g1, g2}

Out[2]= {[image], [image]}
```

## See Also

* [`RadialGradientImage`](https://reference.wolfram.com/language/ref/RadialGradientImage.en.md)
* [`Blend`](https://reference.wolfram.com/language/ref/Blend.en.md)
* [`ConstantImage`](https://reference.wolfram.com/language/ref/ConstantImage.en.md)
* [`RandomImage`](https://reference.wolfram.com/language/ref/RandomImage.en.md)
* [`Image`](https://reference.wolfram.com/language/ref/Image.en.md)
* [`ColorData`](https://reference.wolfram.com/language/ref/ColorData.en.md)
* [`GradientFilter`](https://reference.wolfram.com/language/ref/GradientFilter.en.md)

## Related Guides

* [Creating & Importing Images](https://reference.wolfram.com/language/guide/CreatingAndImportingImages.en.md)

## History

* [Introduced in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) \| [Updated in 2017 (11.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn111.en.md) ▪ [2020 (12.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn122.en.md)