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,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg/1024px-Mona_Lisa,_by_Leonardo_da_Vinci,_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)
CodePudding user response:
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')
CodePudding user response:
you can convert the image to grayscale using PIL.Image.convert:
img = Image.open("/content/Mona_Lisa.png").convert("L")