# Image broke after togray

**URL:** <https://forum.opencv.org/t/image-broke-after-togray/12289>\
**Category:** Python\
**Created:** [March 9, 2023, 10:33am UTC](https://forum.opencv.org/t/image-broke-after-togray/12289 "2023-03-09T10:33:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flash\_Senko](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/flash_senko/32/7335_2.png) [@Flash\_Senko](https://forum.opencv.org/u/Flash_Senko)\
**Post date:** [March 9, 2023, 10:33am UTC](https://forum.opencv.org/t/image-broke-after-togray/12289/1 "2023-03-09T10:33:11Z")

</div>

```auto
img = cv2.imread("heads.png")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

```

 ![result](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/e73c48a11bceacb709ee8e8133c1a9d84ee942e2.png)

The image broke after togray ☹

---

<div class="post-metadata">

**Author:** ![Flash\_Senko](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/flash_senko/32/7335_2.png) [@Flash\_Senko](https://forum.opencv.org/u/Flash_Senko)\
**Post date:** [March 9, 2023, 10:35am UTC](https://forum.opencv.org/t/image-broke-after-togray/12289/2 "2023-03-09T10:35:01Z")

</div>

Original image here.

 ![heads](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/ad304c865289bec484ff911a6a2afbe9af13ee1c.png)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 9, 2023, 12:46pm UTC](https://forum.opencv.org/t/image-broke-after-togray/12289/3 "2023-03-09T12:46:54Z")

</div>

do you realize that your file has an alpha channel?

please consult docs for `imread`.

you need to ask for IMREAD\_UNCHANGED.

further, cvtColor can’t convert to “grayscale with alpha”. that format isn’t defined.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 9, 2023, 2:21pm UTC](https://forum.opencv.org/t/image-broke-after-togray/12289/4 "2023-03-09T14:21:36Z")

</div>

well, the “designer” at the desk next to you might be an idiot, but you can still “repair” it:

```auto

im = cv2.imread("img.png",-1) # load WITH alpha
alpha = im[:,:,3] # extract A
im[alpha==0] = 255 # reset to 'opaque'
i2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) # there we go !

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 9, 2023, 6:13pm UTC](https://forum.opencv.org/t/image-broke-after-togray/12289/5 "2023-03-09T18:13:21Z")

</div>

the file isn’t broken. what you see (backgrounds) is normal behavior for alpha-aware programs.

cvtColor simply doesn’t know how to handle alpha channels.

there is no “grayscale with alpha” format.

if you want to lose all color, you can cvtColor to gray, and then copy that info into the R,G,B channels, keeping the alpha channel. that will look gray, but maintain alpha.

those pixels where alpha == 0 don’t matter at all. those pixels are allowed to have arbitrary color info. it is never actually visible.

the image has 4420000 pixels. 4851 pixels are not totally transparent or totally opaque. those you need to be careful with.
