-1

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.

Vamsi Shankar
  • 71
  • 2
  • 10

4 Answers4

2

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)
mohsinali
  • 266
  • 1
  • 9
  • This worked, but can't I do the same with skimage?? – Vamsi Shankar Feb 04 '20 at 06:08
  • i am not sure i have not worked that much with skimage but here is a [question](https://stackoverflow.com/questions/36206321/scikit-image-saves-binary-image-as-completely-black-image), i think it might be the solution to your problem. – mohsinali Feb 05 '20 at 06:28
0

Doing this task with Pillow will be the most efficient way according to me.

from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')

You can refer to https://stackoverflow.com/a/45338831/9851541

Also, you can try

import imageio
import numpy as np
import matplotlib.pyplot as plt

pic = imageio.imread('DemoImage.png')
gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) 
gray = gray(pic)  
plt.imshow(gray, cmap = plt.get_cmap(name = 'gray'))
Swati Srivastava
  • 992
  • 1
  • 10
  • 16
  • Well this answer is plagiarized, so consider removing this answer before someone reports and you didn't take a full look at my code I have resized the image and I performed the operation on a list of images but not on a single image. – Vamsi Shankar Feb 04 '20 at 06:10
  • Firstly, I did check for solutions online, but first tried it myself and solved the problems I faced, and posted things that worked for me. I'm not a pro, and I have to check for some solutions that I don't know about. And regarding the list of images thing, you can make a function as I did as a lambda function. And then call the function everytime you want to convert to grayscale. – Swati Srivastava Feb 04 '20 at 06:21
  • 1
    It's rather poor advice to suggest adding a completely unnecessary alpha channel... why would you do that? – Mark Setchell Feb 04 '20 at 07:58
0

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)
kalzso
  • 399
  • 2
  • 6
  • 23
0

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)