I tend to use OpenCV
a lot which uses numpy
as a backend, so images are just matrices.
To overlay a binary image onto a background image, you basically do:
overlaycolour = [255,0,0] # for red
for each channel c in 1:3
overlayimage[:,:,c] = (1-alpha)*backgroundimage[:,:,c] +
alpha*binary[:,:,c]*overlaycolour[c]
Here, alpha
is the transparency of the overlay (if alpha
is 1 the overlay is just pasted onto the image; if alpha is 0 the overlay is invisible, and if alpha is in between, the overlay is a bit transparent).
Also, either:
- the binary image (ie image to be overlaid) is normalised to 1s and 0s and the overlay colour is from 0 to 255, OR
- the binary image is 255s and 0s, and the overlay colour is from 0 to 1.
Finally if the background image is grey, turn it into a colour image by just replicating that single channel for each of the red, green, blue channels.
If you wanted to display your background image in a particular colour map you'll have to do that first and feed it in to the overlay function.
I'm not sure how this sort of code is done in PIL, but it shouldn't be too hard.