Question: I have to create a python function called grayscale(image) that converts an image to grayscale. I can only use the from PIL import Image library

I have to create a python function called grayscale(image) that converts an image to grayscale. I can only use the from PIL import Image library and he doesn't want me to use image = image.convert('L'). I am supposed to create a function that adds up the values of RGB and then divides them by 3 to turn them into gray but I can't figure out how to do this. What is one way that I can do this? I have this so far but need to replace my function with something that meets these requirements.

# Import library

from PIL import Image

# Create grayscale function

def grayscale(image):

#Converting the image

image = image.convert('L')

return image

# Open Image and create an output canvas

grayimage = Image.open('beach.jpg')

image_output = Image.open("beach.jpg")

# Calling the function to covert

image_output = grayscale(image_output)

# Save an output image

image_output.save("output.png", "png")

It worked, but I have to change what the function does and I'm not sure where to start.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!