KenobiBastila
KenobiBastila

Reputation: 741

Add a legend (like a matplotlib legend) to an image

Problem : Add a legend (like a matplotlib legend) to an image

Description:

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

My image has , basically, this shape :

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]
    ...

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.

Example Below. Instead of the graphic, i would have an image

Example

Extra Information

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)

The legend should be added in the bottom right of the image.

The reason is because i have to do it, i guess.

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

Upvotes: 2

Views: 1557

Answers (1)

Bill Bell
Bill Bell

Reputation: 21643

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.

Finally, use the facilities in numpy to replace part of the plot array with the legend array, as discussed in this answer

Upvotes: 3

Related Questions