Home > OS >  Convert Black and White image to Gray in python
Convert Black and White image to Gray in python

Time:02-03

how can I convert this black and white image to this gray coloured image?

Black and White Image

1

Grey Scale Image

2

in python

CodePudding user response:

The best that you can do in Python/OpenCV is just change black to gray in your image.

Input:

enter image description here

import cv2
import numpy as np

img = cv2.imread('bw_img.png', 0)
result = img.copy()
result[result==0] = 128

cv2.imwrite('bw_img_grayed.png', result)

cv2.imshow('result', result)
cv2.waitKey(0)

Result:

enter image description here

CodePudding user response:

try this:

import cv2
from PIL import Image

cur_frame = Image.open('image.jpg')
gray_image = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("show image", gray_image)
  • Related