python convert image to grayscale code example

Example 1: convert image to grayscale opencv

img = cv2.imread(path)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Example 2: convert image to grayscale opencv

img_gray_mode = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

Example 3: python image to grayscale

from PIL import Image
img = Image.open("image.jpg")
img.convert("L").save("result.jpg")

Example 4: convert an image to grayscale python using numpy array

1
2
3
4
5
6
7
import numpy as np
from PIL import Image

im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
print(type(im))

gr_im= Image.fromarray(im).save('gr_kolala.png')