I can find many codes that help to convert RGB image to a grayscale image. But none of them shows me how to grayscaling with adjustable percentage like CSS supports.
Percentage: 30
Percentage: 70
Percentage :100
CodePudding user response:
Here is another way to do that in Python/OpenCV. This example is 75% gray (and 25% original).
Input:
import cv2
import numpy as np
# read image
img = cv2.imread("firefox.jpg")
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# make gray into 3 channels
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# blend gray with original
result = cv2.addWeighted(gray, 0.75, img, 0.25, 0)
# save results
cv2.imwrite('firefox_75pct_gray.jpg',result)
# show results
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result: