0

I am having some trouble with converting the pixels in a colored image to gray using Python. Below is the current code I have for reference:

import numpy as np
from PIL import Image
from matplotlib import pyplot

#load the image
color_image = Image.open('...\\Baby_ginger_monkey.jpg')

#Convert image to numpay array
color_image_data = np.asarray(color_image)
print(type(color_image_data))

#summarize image shape
print(color_image_data.shape)

#display the image
pyplot.imshow(color_image)
pyplot.show()

#convert the image into a gray-level image
gray_image_data = color_image_data.mean(axis=2)

The part I'm having trouble with is that I'm not sure how to take the values in the gray_image_data variable and apply it to the pixels of the image. Any form of help would be greatly appreciated.

Daffy04
  • 3
  • 1
  • 1
    Does this answer your question? [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) – My Work Feb 05 '21 at 21:33
  • This does help out a lot. Thank you very much – Daffy04 Feb 05 '21 at 21:58

1 Answers1

0

You can use OpenCV:

import cv2
image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Frederick
  • 450
  • 4
  • 22
  • I'll try this out. I think I will need to download the cv2 though. I'll let you know my results after I try it. – Daffy04 Feb 05 '21 at 21:35
  • Just use pip install opencv-python or pip3 – Frederick Feb 05 '21 at 21:40
  • I keep getting the error message ERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none) ERROR: No matching distribution found for opencv-python – Daffy04 Feb 05 '21 at 21:46
  • Are you on Windows/Linux or MacOS? – Frederick Feb 05 '21 at 21:47
  • Here are many ways to solve it: https://stackoverflow.com/questions/45293933/could-not-find-a-version-that-satisfies-the-requirement-opencv-python – Frederick Feb 05 '21 at 21:48