Two dimensional color ramp (256x256 matrix) interpolated from 4 corner colors
<p>What I want to achieve is to programmatically create a two-dimensional color ramp represented by a 256x256 matrix of color values. The expected result can be seen in the attached image. What I have for a starting point are the 4 corner colors of the matrix from which the remaining 254 colors inbetween should be interpolated. While I had some success for interpolating the colors for one axis, the two-dimensional calculation provides me some bad headaches. While the image seems to have a non-linear color gradient, I would be happy with a linear one.</p>

<p>If you could give me some hints how to do this with numpy or other tools I`ll be more than thankful.</p>

<p><a href="http://i.stack.imgur.com/o1BUK.png"><img src="http://i.stack.imgur.com/o1BUK.png" alt="enter image description here"></a></p>

<p>Here's a very short way to do it with <strong>ImageMagick</strong> which is installed on most Linux distros and is available for OSX and Windows. There are also Python bindings. Anyway, just at the command line, create a 2x2 square with the colours at the 4 corners of your image, then let ImageMagick expand and interpolate up to the full size:</p>

<pre><code>convert \( xc:"#59605c" xc:"#ebedb3" +append \) \
        \( xc:"#69766d" xc:"#b3b3a0" +append \) \
        -append -resize 256x256 result.png
</code></pre>

<p><a href="http://i.stack.imgur.com/IfObm.png" rel="nofollow"><img src="http://i.stack.imgur.com/IfObm.png" alt="enter image description here"></a></p>

<p>The first line makes a 1x1 pixel of each of your top-left and top-right corners and appends the two side by side. The second line makes a 1x1 pixel of each of your bottom-left and bottom-right corners and appends them side by side. The final line appends the bottom row below the top row and enlarges by interpolation to 256x256.</p>

<p>If you want to better understand what's going on, here is the same basic image but scaled up using nearest neighbour rather than interpolation:</p>

<pre><code>convert \( xc:"#59605c" xc:"#ebedb3" +append \) \
        \( xc:"#69766d" xc:"#b3b3a0" +append \) \
        -append -scale 20x20 result.png
</code></pre>

<p><a href="http://i.stack.imgur.com/NmzWs.png" rel="nofollow"><img src="http://i.stack.imgur.com/NmzWs.png" alt="enter image description here"></a></p>

python numpy image-processing