Home > Blockchain >  Python grayscaling rgb image with percentage
Python grayscaling rgb image with percentage

Time:04-07

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. enter image description here

Percentage: 30

enter image description here

Percentage: 70

enter image description here

Percentage :100

enter image description here

CodePudding user response:

Here is another way to do that in Python/OpenCV. This example is 75% gray (and 25% original).

Input:

enter image description here

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:

enter image description here

  • Related