1

I have some rgb images and I want to change them to gray. I used skimage.color.rgb2gay function. my color images have values between 0 and 255 but after changing them to gray the value are between 0 and 1 and it seems they normalize. could you please tell me what is the problem? the input image's type is uint8 and I expect the gray image has unit8 type not float64! this is the code I used.

import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2gray

original = data.astronaut()
grayscale1 = rgb2gray(original)

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].imshow(original)
ax[0].set_title("Original")
ax[1].imshow(grayscale1, cmap=plt.cm.gray)
ax[1].set_title("Grayscale")

fig.tight_layout()
plt.show()
david
  • 1,255
  • 4
  • 13
  • 26
  • it seems sklimage doesn't have function to convert directly to `unit8`. You would have to multiply by 256 and convert to `uint8`. Module `PIL/pillow` can get numpy array with RGB values, convert to RGB `Image` object, convert it to gray Image `uint8` (mode "L") and gives back numpy array. – furas Jul 14 '19 at 02:57
  • [How can I convert an RGB image into grayscale in Python?](https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python) – furas Jul 14 '19 at 02:58
  • `grayscale = (grayscale*256).astype('uint8')` – furas Jul 14 '19 at 03:00

0 Answers0