-1

for some reason this isn't working.

i may be making a silly mistake somewhere. please help

# importing modules
import urllib.request
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from PIL import Image

#dowload mona lisa image

urllib.request.urlretrieve(
  'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/1024px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg',
   "Mona_Lisa.png")

#open the file

img = Image.open("/content/Mona_Lisa.png")

#convert to from rgba to rgb

rgb_image = img.convert('RGB')
rgb_image_rgb = np.array(rgb_image)

#show image

plt.imshow(rgb_image_rgb, cmap = cm.Greys_r)
sahuno
  • 321
  • 1
  • 8
  • did you check ImageOps.grayscale()? which also from pillow library – Mustafa KÜÇÜKDEMİRCİ Nov 18 '22 at 08:51
  • 2
    You've taken a JPEG image and arbitrarily pretended it's a PNG - kind of like sticking a Ferrari badge on your Fiat, it doesn't make it a Ferrari. Then you've saved it in the current directory but opened a completely different file in your top-level `/content` directory. Then you've made it 3-channel RGB for no apparent reason when you really want 1-channel greyscale. – Mark Setchell Nov 18 '22 at 09:26

2 Answers2

3

have you tried this answer ?

How can I convert an RGB image into grayscale in Python?

from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
Chris
  • 379
  • 1
  • 2
  • 10
1

you can convert the image to grayscale using PIL.Image.convert:

img = Image.open("/content/Mona_Lisa.png").convert("L")
Runinho
  • 439
  • 1
  • 6