Convert image to grayscale in python

Convert image to grayscale in Python

A grayscale image is an image that simply contains only the shades of gray and no other color. The main advantage of using a grayscale image over a colored image is that it brings down the computational requirements, increases the speed of processing, and makes a significant impact on the complexity of the algorithm.

This tutorial discusses the different ways available to convert image to grayscale in Python.

Using the .convert() function from the Pillow Imaging Library (Fork) to convert image to grayscale in Python.

The Pillow Imaging Library is an open-source additional library that can be added to Python and is utilized for providing the functions that help in the opening, saving, and manipulating different image file formats in Python. This library is available on all Windows, Linux, and MacOS X.

The .convert() function takes an image as its parameter and is utilized to convert the given image into an image type that the user specifies in its mode parameter.

The .convert() function provides different modes for the process of conversion of an image to grayscale in Python. Below are the modes that are usable for this process.

  • 1 : It can be defined as a true black and white image mode that only has a 1-bit channel and takes only two values, either full black or full white.
  • L : It is an abbreviation for Luminance Channel. It is an 8-bit B&W/grayscale image mode. It usually takes up any value in the range of 0 to 255, which represents all the colors that are white, black with all the shades of gray in between them.
  • LA : An abbreviation for Luminance with Alpha Transparency Channel. It is utilized when the converted image needs to be successfully converted with transparency. This mode only provides support for images with the .png and the .gif file extensions.

The following code uses the .convert() function from the Pillow Imaging Library (Fork) to convert image to grayscale in Python.

Explanation
  • The original image is opened and stored as orig in the Python code using the Image.open() function.
  • The image is converted to grayscale and stored in the variable gsi. In this code, we use the 8-bit L mode.
  • Finally, the picture stored in the variable gsi is saved and stored on to the system.

The above code converts a colored image into a grayscale image. Here is a comparison between both the pictures.

Original Image :

Converted Image (After grayscale):

Using the OpenCV library functions to convert image to grayscale in Python.

Another method to convert an image to grayscale is with the use of several functions from the OpenCV library. OpenCV is an enormous library that provides functions that are used for Machine learning, Computer Vision, and Image Processing. It is supported not only by Python but also other languages like Java, C++, etc.

OpenCV provides several functions for image processing which are used here to convert the given image to grayscale. The module is not in-built and needs to be imported to the Python code to successfully run the code without any errors.

The following code uses the OpenCV library functions to convert image to grayscale in Python.

The output of the code would be the same as that of the method mentioned in the article above.

Explanation
  • The OpenCV module or cv2 is imported to the Python code first.
  • The original image is first loaded to the program code using the imread() function.
  • The loaded image is then converted to grayscale with the help of the .cvtColor() function.
  • The converted file is then stored on the disk with the help of the imwrite() function.

Using the color.rgb2gray() function from the scikit-image module to convert image to grayscale in Python.

scikit-image is an open-source and free-to-use library that provides various functions only for the purpose of image processing in Python. The color and io sub-modules from the skimage module need to be imported to the Python code in order to use the color.rgb2gray() function.

The color.rgb2gray() function is utilized to take an image in the RGB format as an argument and return a grayscale copy of the original image.

The following code uses the color.rgb2gray() function from the scikit-image module to convert image to grayscale in Python.

The output of the above code would be the same as the other codes mentioned above in the article.

Explanation
  • color and io are imported from the skimage module to run the code without any errors.
  • The io.imread() function is utilized to load the original image to the Python code.
  • The color.rgb2gray() function takes in the original image and returns a grayscale image to the Python code, which is stored in the variable gsi.

Using the Conversion Formula along with the matplotlib library to convert image to grayscale in Python.

There exists a standard conversion formula for converting images in RGB format to grayscale directly. The determined formula for this is gsi = 0.2989 * R + 0.5870 * G + 0.1140 * B.

Along with the standard conversion formula, we also need to use and import the matplotlib library in order to successfully implement this method.

Matplotlib is a cross-platform library that is mainly used for graphical plotting and data visualization in Python and the NumPy library which is an extension of Python.

The following code uses the Conversion Formula along with the matplotlib library to convert image to grayscale in Python.

The output of the above code would be the same as the other codes mentioned above in the article.

Explanation
  • The matplotlib module is imported to the Python code to successfully run the code without any errors in Python.
  • The image is loaded to the code using the mpimg.read() function.
  • Then, the dimension matrices of the RGB image are taken.
  • Then, the given conversion formula is applied to the dimension matrices in order to convert the image to grayscale.
  • The converted image is then stored and shown using the plt.imshow() function.

That’s all about how to convert image to grayscale in Python.

Related Posts

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Auto clicker in Python
    30 August

    How to make auto clicker in Python

    Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write variable to file in Python
    30 August

    Write variable to file in python

    Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.