# Color a black/white .tif image with 3 colors

**URL:** <https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631>\
**Category:** Python\
**Tags:** programming, core\
**Created:** [August 6, 2021, 11:08am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631 "2021-08-06T11:08:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [August 6, 2021, 11:08am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/1 "2021-08-06T11:08:57Z")

</div>

So, I have a black/white UAV .tif image and I want to group the values of the pixels (ranging from 0 - 300) to 3 groups of colors. So for instance pixels with values from 0-100 to transformed to Green color, pixels from 101-200 to transformed to Red color, and pixels from 201-300 to transformed to Blue color. I am searching on google but I do not find something similar… Any idea how to do this? I am working in python 3.

---

<div class="post-metadata">

**Author:** ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)\
**Post date:** [August 6, 2021, 11:28am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/2 "2021-08-06T11:28:55Z")

</div>

See [OpenCV: ColorMaps in OpenCV](https://docs.opencv.org/master/d3/d50/group __imgproc__ colormap.html#gadf478a5e5ff49d8aa24e726ea6f65d15)

---

<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:** [August 6, 2021, 3:37pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/3 "2021-08-06T15:37:28Z")

</div>

or simply numpy:

```python
assert input.shape == (height, width) # single channel

output = np.zeros((height, width, 3), dtype=np.uint8)

output[(input <= 50)] = (255,0,255)
output[(input > 100) & (input <= 150)] = (255,255,0)
output[(input > 200)] = (0,255,255)

```

_always_ work through the documentation and tutorials of the technologies you use. numpy is a part of that.

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [August 6, 2021, 4:15pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/4 "2021-08-06T16:15:41Z")

</div>

This is the code:

```auto
import cv2
 
input = cv2.imread('UAV_image.jpg')

height = 1390
width = 1300

assert input.shape == (height, width) # single channel

output = np.zeros((height, width, 3), dtype=np.uint8)

output[(input <= 50)] = (255,0,255)
output[(input > 100) & (input <= 150)] = (255,255,0)
output[(input > 200)] = (0,255,255)

cv2.imwrite('UAV_image_3_colors.jpg', image_gray)

```

Any idea why it prints this??:

```auto
    assert input.shape == (height, width) # single channel
AssertionError

```

Sorry I am very new…all day I am over the google…I am testing it firstly with a jpeg…

---

<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:** [August 6, 2021, 7:33pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/5 "2021-08-06T19:33:55Z")

</div>

your picture isn’t single-channel

or the width and height don’t match

look at `input.shape`

these are basic debugging skills

[![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8717c49254444c07ce00a5352b0418ad0aaf766e.jpeg "10 ways to debug Python code") ](https://www.youtube.com/watch?v=cokP4XAhcwo)

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [August 6, 2021, 9:06pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/6 "2021-08-06T21:06:49Z")

</div>

I changed 2-3 images. The image is black/white so 1 channel and the height/width correct… I cannot figure out what is wrong!

---

<div class="post-metadata">

**Author:** ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)\
**Post date:** [August 6, 2021, 9:27pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/7 "2021-08-06T21:27:47Z")

</div>

input = cv2.imread(‘UAV\_image.jpg’,cv2. IMREAD\_GRAYSCALE)

---

<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:** [August 6, 2021, 9:33pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/8 "2021-08-06T21:33:54Z")

</div>

ah yes, imread does autoconversion to BGR, if I remember correctly… `IMREAD_UNCHANGED` would be useful.

or what sturkmen recommended.

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [August 6, 2021, 9:44pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/9 "2021-08-06T21:44:18Z")

</div>

I changed the last line to this:

```auto
cv2.imwrite('UAV_image_3_colors.jpg',0)

```

because the previous didn’t run, but I do not see anywhere in the folder the output (stored) image…

---

<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:** [August 7, 2021, 7:43am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/10 "2021-08-07T07:43:05Z")

</div>

> [@mike123](#):
>
> `cv2.imwrite('UAV_image_3_colors.jpg',0)`

no sense at all in that line

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [August 7, 2021, 7:44am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/11 "2021-08-07T07:44:04Z")

</div>

Thanks to all! You are amazing… It works!

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [November 5, 2021, 1:29pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/13 "2021-11-05T13:29:01Z")

</div>

Why did you use bitwise and (`&`) and not logical and (`and`)?

---

<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:** [November 5, 2021, 6:15pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/14 "2021-11-05T18:15:42Z")

</div>

those are numpy arrays. they don’t respond well to the `and` operator. try it.

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [November 5, 2021, 7:35pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/15 "2021-11-05T19:35:00Z")

</div>

When I increase the code for more colors, 5, I lose the 2 last colors…I do not know why! Any ideas??

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [March 5, 2022, 5:22pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/16 "2022-03-05T17:22:55Z")

</div>

I am using this code after so many months, and it prints me the same error:

```auto
    assert input.shape == (height, width, 4) # single channel
AssertionError

```

I checked shape and returns me `()` What could be possibly wrong? I checked the path it is correct. I used absolute path, the same error!!!

---

<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 5, 2022, 6:21pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/17 "2022-03-05T18:21:24Z")

</div>

> [@mike123](#):
>
> I checked shape and returns me `()`

it means, your image is empty / invalid / was not read

besides that:

> [@mike123](#):
>
> `assert input.shape == (height, width, 4) # single channel`

no you’re assuming 4 channels here, not a single one  
(mean what you say, say what you mean)

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [March 5, 2022, 6:27pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/18 "2022-03-05T18:27:33Z")

</div>

Sorry for the wrong comment. I forgot it there. The UAV .tif has RGBA (4 channels)… What is wrong with the image? I checked another one, the same error…

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [October 21, 2022, 2:06pm UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/19 "2022-10-21T14:06:21Z")

</div>

```auto
assert input.shape == (height, width) # single channel

output = np.zeros((height, width, 3), dtype=np.uint8)

output[(input <= 50)] = (255,0,255)
output[(input > 100) & (input <= 150)] = (255,255,0)
output[(input > 200)] = (0,255,255)

```

What should I change to the above code for running it in C++ ? I need the related code in C++ Thanks!!

---

<div class="post-metadata">

**Author:** ![mike123](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mike123](https://forum.opencv.org/u/mike123)\
**Post date:** [October 23, 2022, 9:48am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/20 "2022-10-23T09:48:11Z")

</div>

I have installed opencv library and I am using it on python. Do I need to install a different library If I want to use it on C++ ? I am trying to accelerate the above mentioned python code. But as I have read C++ does not make something more than python code, because python calls C++ code on the above example…Am I right? Any ideas how to accelerate the above code??

---

<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:** [October 23, 2022, 10:58am UTC](https://forum.opencv.org/t/color-a-black-white-tif-image-with-3-colors/4631/21 "2022-10-23T10:58:31Z")

</div>

yes, most operations in Python just call compiled C/C++ code.

you can’t “accelerate” infinitely. this is always true.

you can only accelerate something that (1) is inefficient and (2) can feasibly be made more efficient.

first you need to be aware of _why_ you want to accelerate. not everything is _worth_ optimizing. then you should explain your reasoning so others understand too.

then you need to _measure_. without numbers to tell you _what_ to scrutinize for performance, you would likely waste effort optimizing the wrong things.
