How can I convert an RGB image into grayscale in Python?
How about doing it with Pillow:
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA
:
img = Image.open('image.png').convert('LA')
Using matplotlib and the formula
Y' = 0.2989 R + 0.5870 G + 0.1140 B
you could do:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Converting RGB to grayscale python
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)
Convert Color Image to Grayscale in Python without OpenCV
You could write it, based on the conversion formula "Standard" RGB to Grayscale Conversion:
import numpy as np
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
How to convert RGB images to grayscale, expand dimensions of that grayscale image to use in InceptionV3?
In ImageDataGenerator
, you can pass a preprocessing function. Use the functions tf.image.rgb_to_grayscale
and tf.image.grayscale_to_rgb
to do the transformation:
def to_grayscale_then_rgb(image):
image = tf.image.rgb_to_grayscale(image)
image = tf.image.grayscale_to_rgb(image)
return image
tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1/255,
preprocessing_function=to_grayscale_then_rgb
)
RGb to gray conversion in CV2
The default option for cmap in plt.imshow
is viridis
. Use plt.imshow(gray, cmap='gray')
for grayscale images. If you save the image using cv2.imwrite
you can see the image has been converted to grayscale.
how to convert rgb image To grayscale in python
First of all, it seems like you are working with an array of 267 of 100x100 RGB images here. I am assuming you are using a NumPy array. In order to convert the images to grayscale you can use the method proposed in this answer:
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
x_train_gray = rgb2gray(x_train)
Note that this works for all images in one pass and the resulting shape should be (267, 100, 100)
. However, np.imshow
only works for one image at a time so to plot an image in grayscale you can do the following:
plt.imshow(x_train_gray[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Related Topics
How to Copy a String to the Clipboard
Speed Up Millions of Regex Replacements in Python 3
How to Use the Apply() Function For a Single Column
How to Install Pip on Macos or Os X
Updating Gui Elements in Multithreaded Pyqt
How to Read a Text File into a String Variable and Strip Newlines
How to Get the Path and Name of the File That Is Currently Executing
What Does It Mean If a Python Object Is "Subscriptable" or Not
Understanding Repr( ) Function in Python
Convert String to Variable Name in Python
How to Create a List of Random Numbers Without Duplicates
How to Chain the Movement of a Snake'S Body
What Is the Eafp Principle in Python
Make a Dictionary With Duplicate Keys in Python
What Is the Syntax Rule For Having Trailing Commas in Tuple Definitions