# Converting RGBA image to Gray scale and then binary

**URL:** <https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258>\
**Category:** Python Help\
**Created:** [August 21, 2021, 7:48pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258 "2021-08-21T19:48:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joni](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Joni](https://discuss.python.org/u/Joni)\
**Post date:** [August 21, 2021, 7:48pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/1 "2021-08-21T19:48:45Z")

</div>

Hi,

I need to convert a RGBA image in to Gray scale and then to binary with a threshold but I am not able to do that in python. Here is my code . The error which I get is “AttributeError: module ‘PIL.Image’ has no attribute ‘rgb2gray’”

```auto
# Import libraries 
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

# Reading an Image
image = Image.open('Board1_1.png')
image_gray = Image.rgb2gray(image)

```

---

<div class="post-metadata">

**Author:** ![petersuter](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@petersuter](https://discuss.python.org/u/petersuter)\
**Post date:** [August 21, 2021, 8:13pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/2 "2021-08-21T20:13:19Z")

</div>

Hi,

According to [this](https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python) you can use [image\_gray = image.convert(‘LA’)](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.convert).

Other libraries like `skimage` or `scipy` call this functionality `rgb2gray` but the `PIL` library you use does not.

---

<div class="post-metadata">

**Author:** ![Joni](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Joni](https://discuss.python.org/u/Joni)\
**Post date:** [August 21, 2021, 8:38pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/3 "2021-08-21T20:38:46Z")

</div>

What is the difference between the arguments ‘L’ and ‘LA’ inside the function image.convert ?

---

<div class="post-metadata">

**Author:** ![Joni](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Joni](https://discuss.python.org/u/Joni)\
**Post date:** [August 21, 2021, 8:45pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/4 "2021-08-21T20:45:58Z")

</div>

When I convert the image to gray scale using the argument ‘LA’, I get the dimension of the gray scale image is rows x col x 2 which I don’t understand why there are still two matrix instead of one matrix

---

<div class="post-metadata">

**Author:** ![petersuter](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@petersuter](https://discuss.python.org/u/petersuter)\
**Post date:** [August 21, 2021, 9:32pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/5 "2021-08-21T21:32:10Z")

</div>

> `LA` mode has luminosity (brightness) and alpha

Maybe you want only ‘L’.

---

<div class="post-metadata">

**Author:** ![Joni](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Joni](https://discuss.python.org/u/Joni)\
**Post date:** [August 22, 2021, 7:36am UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/6 "2021-08-22T07:36:07Z")

</div>

I have used the argument ‘LA’ and get the gray scale image which I plotted. I don’t know why only ‘L’ does not work as I get dark image.

` image_gray = image.convert('LA')`

The gray scale image has two dimension i.e., two matrix (row x col x 2). I can plot it, it looks like a gray scale image. Now I need to binarize it with threshold and plot a histogram. I know the theory but could not find the which function in python can convert gray scale image to binary and then make its histogram.

I have tried this function  
'(thresh, im\_bw) = cv2.threshold(image\_gray, 128, 255, cv2.THRESH\_BINARY | cv2.THRESH\_OTSU)  
’  
I get this error

> cv2.error: OpenCV(4.5.3) 👎 error: (-5:Bad argument) in function ‘threshold’  
> Overload resolution failed:
> 
> - src is not a numpy array, neither a scalar
> - Expected Ptrcv::UMat for argument ‘src’

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [August 26, 2021, 10:25pm UTC](https://discuss.python.org/t/converting-rgba-image-to-gray-scale-and-then-binary/10258/7 "2021-08-26T22:25:22Z")

</div>

FWIW, PIL.ImageOps has a grayscale function. It takes an image and returns an image.

```auto
>>> help(PIL.ImageOps.grayscale)
Help on function grayscale in module PIL.ImageOps:

grayscale(image)
    Convert the image to grayscale.
    
    :param image: The image to convert.
    :return: An image.

```

It’s not an object interface, but I wouldn’t be surprised if it was wrapped somewhere in an object-oriented API. Figuring that out is left as an exercise for the reader.
