Add a legend (like a matplotlib legend) to an image
<p><strong>Problem : Add a legend (like a matplotlib legend) to an image</strong></p>

<p><strong>Description:</strong></p>

<p>I have an image as a numpy array uint8.
I want to add a legend to it, exactly as matplotlib does with its plots.</p>

<p>My image has , basically, this shape :</p>

<pre><code>output_image = np.empty(shape=(x,x, 4), dtype=np.uint8)
    # B-G-R-A
blue = [255, 0, 0, 255]
green = [0, 255, 0, 255]
red = [0, 0, 255, 255]
orange = [0, 128, 255, 255]
black = [0, 0, 0, 255]
    ...
</code></pre>

<p>The colors above are added.
Then the image is returned. And when it is returned by the method, i would like to add a graphic to it.</p>

<p><strong>Example Below. Instead of the graphic, i would have an image</strong></p>

<p><a href="https://i.stack.imgur.com/6c6KQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/6c6KQ.png" alt="Example"></a></p>

<p><strong>Extra Information</strong></p>

<p>The output is a numpy array with values ranging from 0 to 255.
Each pixel, value in the array, is formed by a 4-D array ( Blue-Green-Red-Alpha)</p>

<p>The legend should be added in the bottom right of the image.</p>

<p>The reason is because i have to do it, i guess.</p>

<p>Basically the current output is the numpy array, which i later use for other purposes.</p>

<p>Your question itself makes it clear that you know how to make an image in a numpy array. Now make your legend using the same techniques in a smaller numpy array.</p>

<p>Finally, use the facilities in numpy to replace part of the plot array with the legend array, as discussed in <a href="http://stackoverflow.com/questions/26506204/replace-sub-part-of-matrix-by-another-small-matrix-in-numpy">this answer</a></p>

python numpy matplotlib