Search code examples
pythonimage-processingpython-imaging-libraryrgbgrayscale

Converting image to Greyscale


I know that we can convert RGB image to greyscale by using PIL or PILLOW. We can do this by importing Image. One way that can be used to do this is as follows.

from PIL import Image img=Image.open('Sample.jpg').convert('LA') img.save('greyscale.png')

Can we convert RGB image to greyscale without using a python package? I wan to learn that way. Any help, how can I do this?


Solution

  • if you look a data from a RGB image with Pillow you will get a turple with 3 values, or 4 if you read in RGBA mode. In grayscale (L) you have only 1 value. With Python you have so many libs to convert a RGB to L, and they have different ways/methods. To convert RGB to L you need make the sum of RGB and have manys formulas like

     (max(R, G, B) + min(R, G, B)) / 2 #lightness method
     (R + G + B) / 3 # average method
     0.21 R + 0.72 G + 0.07 B # luminosity method
    

    but to create a image without a lib I don't know, because I create a method to draw a image using Pillow