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 super short solution using the <a href="http://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.ndimage.interpolation.zoom.html" rel="nofollow">zoom function</a> from <code>scipy.ndimage</code>. I define a 2x2 RGB image with the intial colors (here random ones) and simply zoom it to 256x256, <code>order=1</code> makes the interpolation linear. Here is the code :</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt 

im=(np.random.rand(2,2,3)*255).astype(np.uint8)

from scipy.ndimage.interpolation import zoom
zoomed=zoom(im,(128,128,1),order=1)

plt.subplot(121)
plt.imshow(im,interpolation='nearest')
plt.subplot(122)
plt.imshow(zoomed,interpolation='nearest')
plt.show()
</code></pre>

<p>Output:</p>

<p><a href="http://i.stack.imgur.com/mMVYM.png" rel="nofollow"><img src="http://i.stack.imgur.com/mMVYM.png" alt="output from the script"></a></p>

python numpy image-processing