Alessandro
Alessandro

Reputation: 794

How to draw a colored pixel on a grayscale image?

I have a bidimensional ndarray. Each element is an integer. I display the matrix in this way:

plt.figure()
plt.imshow(img, cmap='Greys')

where img is the bidimensional array. Now I would to highlight some points on the image: for example I would that the pixel in (x,y) is red (I don't want change the real pixel value, but only display the image with the red pixel).

Any suggestion?

Upvotes: 4

Views: 5854

Answers (1)

P. Camilleri
P. Camilleri

Reputation: 13218

Here is a code snippet to illustrate the process (the idea is to play with RGB channels):

import numpy as np
import matplotlib.pyplot as plt
im = np.random.randint(0, 255, (16, 16))
I = np.dstack([im, im, im])
x = 5
y = 5
I[x, y, :] = [1, 0, 0]
plt.imshow(I, interpolation='nearest' )
plt.imshow(im, interpolation='nearest', cmap='Greys')

where I have arbitrarily chosen x and y equal to 5 (original image being 16x16)

the last line shows im has been untouched, and the one before that gives the result you want.

Output:

enter image description here

enter image description here

Upvotes: 4

Related Questions