Search code examples
pythonpython-3.xlistopencvpython-imaging-library

Converting RGB to grayscale python


I have been converting rgb images to grayscale images, below is the code

import numpy
import glob
import cv2
import csv
import math
import os
import string
from skimage.color import rgb2gray
from PIL import Image

mylist = [f for f in glob.glob("*.jpg")]

for imagefile in mylist:
    img_color = cv2.imread(imagefile)
    image = cv2.resize(img_color,(100,100),interpolation = cv2.INTER_AREA)
    img_gray = rgb2gray(image)
    img_gray.flatten()

Im not getting the new image saved into my current folder. Can anyone help me regarding this.


Solution

  • I think it is because of skimage. why dont you use just opencv.

    import numpy
    import glob
    import cv2
    import csv
    import math
    import os
    import string
    from skimage.color import rgb2gray
    from PIL import Image
    
    mylist = [f for f in glob.glob("*.jpg")]
    
    for imagefile in mylist:
        img_color = cv2.imread(imagefile)
        image = cv2.resize(img_color,(100,100),interpolation = cv2.INTER_AREA)
        img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        #img_gray = rgb2gray(image)
        img_gray.flatten()
        cv2.imwrite("gray"+imagefile,img_gray)