简体   繁体   中英

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.

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)

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.

You could also do this in one step if you want by adding an additional parameter to imread like this:

gray = cv2.imread(imagefile, cv2.IMREAD_GRAYSCALE)
cv2.imshow('gray', gray)

Basically you need to add code for saving image file:

import numpy
import glob
import cv2
import csv
import math
import os
import string
from skimage.color import rgb2gray
from skimage.io import imsave    # skimage method for saving image
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()
    imsave("gray"+imagefile, img_gray)  # save the image

Or using opencv

img_gray = img_gray*255
cv2.imwrite("gray"+imagefile, img_gray.astype("uint8"))

But if you want to implement this entirely in skimage

import glob
from skimage.io import imread, imsave
from skimage.color import rgb2gray
from skimage.transform import resize

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

for imagefile in mylist:
    img_color = imread(imagefile)
    image = resize(img_color, (100, 100))
    img_gray = rgb2gray(image)
    img_gray.flatten()
    imsave("gray" + imagefile, img_gray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM