Skip to content
Advertisement

Black and white color addition

When red is mixed with green, I get yellow as expected.

RGB for Red:   [255, 0, 0] 
RGB for Green: [0, 255, 0]
Result:        [255, 255, 0]

But when white is mixed with black, I should get normally grey but I get white. Shouldn’t I get grey?

RGB for Black: [0, 0, 0] 
RGB for White: [255, 255, 255]
Result:        [255, 255, 255] 

Here is the code:

from PIL import Image, ImageChops
import math
import matplotlib.pylab as plt

im1= Image.open(r'.red.jpg')
im2= Image.open(r'.green.jpg')

result = ImageChops.add(im1, im2)

plt.imshow(result)
 

Advertisement

Answer

I think what @Cris Luengo said (“If you want to get gray, average the white and black pixels together”) is valid; Also I think one additional thing can be a MixFactor.

You can use OpenCV for this.

Imports:

import sys
import cv2
import numpy as np

Load image:

im = cv2.imread(sys.path[0]+'/im.png')

Main code:

color=[0,0,0]
mixFactor=.5
im = (1-mixFactor)*im+[(mixFactor)*x for x in color]

My input values for color:

[0, 0, 0] black
[255, 255, 255] white
[255, 0, 0] blue (BGR)
[0, 255, 0] green   
[0, 0, 255] red       

I draw the first colorful image using a graphical software; The other 8 created with this Python code.

enter image description here


Credit: The text on the image is written using the default Hershey font included in OpenCV. And you can read more here and here.


Update:
If you want to use imshow for the output of this blending method; use it like this:

cv2.imshow("preview", im/255)
Advertisement