Categories
Python Answers

How to convert an RGB image into grayscale in Python?

Spread the love

Sometimes, we want to convert an RGB image into grayscale in Python.

In this article, we’ll look at how to convert an RGB image into grayscale in Python.

How to convert an RGB image into grayscale in Python?

To convert an RGB image into grayscale in Python, we can use the convert method.

For instance, we write

from PIL import Image

img = Image.open('image.png').convert('L')
img.save('greyscale.png')

to call Image.open with the image path to open the image.

Then we call convert with 'L' to convert the image to grayscale.

Finally, we call save to save the image to a file.

Conclusion

To convert an RGB image into grayscale in Python, we can use the convert method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *