When red is mixed with green, I get yellow as expected.
JavaScript
x
4
1
RGB for Red: [255, 0, 0]
2
RGB for Green: [0, 255, 0]
3
Result: [255, 255, 0]
4
But when white is mixed with black, I should get normally grey but I get white. Shouldn’t I get grey?
JavaScript
1
4
1
RGB for Black: [0, 0, 0]
2
RGB for White: [255, 255, 255]
3
Result: [255, 255, 255]
4
Here is the code:
JavaScript
1
12
12
1
from PIL import Image, ImageChops
2
import math
3
import matplotlib.pylab as plt
4
5
im1= Image.open(r'.red.jpg')
6
im2= Image.open(r'.green.jpg')
7
8
result = ImageChops.add(im1, im2)
9
10
plt.imshow(result)
11
12
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:
JavaScript
1
4
1
import sys
2
import cv2
3
import numpy as np
4
Load image:
JavaScript
1
2
1
im = cv2.imread(sys.path[0]+'/im.png')
2
Main code:
JavaScript
1
4
1
color=[0,0,0]
2
mixFactor=.5
3
im = (1-mixFactor)*im+[(mixFactor)*x for x in color]
4
My input values for color:
JavaScript
1
6
1
[0, 0, 0] black
2
[255, 255, 255] white
3
[255, 0, 0] blue (BGR)
4
[0, 255, 0] green
5
[0, 0, 255] red
6
I draw the first colorful image using a graphical software; The other 8 created with this Python code.
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:
JavaScript
1
2
1
cv2.imshow("preview", im/255)
2