# How can I get the bands of a .tiff UAV image using tifffile?

**URL:** <https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880>\
**Category:** Uncategorized\
**Created:** [October 27, 2021, 10:04am UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880 "2021-10-27T10:04:31Z")\
**Posts on this page:** 18\
**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:** [October 27, 2021, 10:04am UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/1 "2021-10-27T10:04:31Z")

</div>

I have a .tiff UAV image and I need to get the R,G,B bands with georeferencing using tifffile. Any idea how do I achieve this?

---

<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 27, 2021, 5:38pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/2 "2021-10-27T17:38:13Z")

</div>

this? [tifffile · PyPI](https://pypi.org/project/tifffile/)  
or that (which is the above)? [Module: external.tifffile — skimage v0.14.3 docs](https://scikit-image.org/docs/0.14.x/api/skimage.external.tifffile.html)

---

<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 29, 2021, 12:12pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/4 "2021-10-29T12:12:00Z")

</div>

I managed to get the 3 bands of the UAV .tiff and did some calculations (such as result = Green/ Red) with rasterio. But when I try to open the new .tif image I get this error:

```auto
    self.img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(3.4.14) /tmp/pip-req-build-m7h7rfem/opencv/modules/imgproc/src/color.simd_helpers.hpp:88: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1

```

Any idea how to fix this?  
The new .tif has only one channel! And I use PyQt to display it…

---

<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 29, 2021, 3:57pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/5 "2021-10-29T15:57:09Z")

</div>

that means `frame` is single-channel.

why is it single-channel?

why do you expect it to be `BGR`?

---

<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 29, 2021, 4:31pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/6 "2021-10-29T16:31:42Z")

</div>

Sometimes I open RGB tif images, sometimes 4-channels tif images and sometimes 1-channel tif images…How can I achieve that?

---

<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 29, 2021, 5:06pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/7 "2021-10-29T17:06:08Z")

</div>

check the `.shape` (height, width, channels), do stuff according to situation

---

<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 30, 2021, 10:32am UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/8 "2021-10-30T10:32:57Z")

</div>

Ok, so how do I make it accept 1-band tiff images? Which is the suitable flag?

---

<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 30, 2021, 11:59am UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/9 "2021-10-30T11:59:46Z")

</div>

> [@mike123](#):
>
> `COLOR_BGR2RGB`

that would require the source data to be BGR, i.e. 3-channel.

if you don’t have 3-channel data, you shouldn’t _expect_ that color conversion code to work.

if you have 1-channel data instead, use a _different_ color conversion code… perhaps one that reads like “gray to …”

---

<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 30, 2021, 5:08pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/10 "2021-10-30T17:08:42Z")

</div>

The code I use is from google search/github. I don’t want to do any color conversion, just show the image on the PyQt window, either 4-band, 1-band, 3-band. I do not want to do any color conversion…Any ideas?

---

<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 30, 2021, 5:42pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/11 "2021-10-30T17:42:40Z")

</div>

okay, so what formats does Qt like and how do you give the data to Qt?

---

<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 30, 2021, 8:46pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/12 "2021-10-30T20:46:25Z")

</div>

> [@mike123](#):
>
> COLOR\_BGR2RGB

1. It reads RGBA and gray scale. I choose the file from a open file button.
2. I read the channels like this: `height, width, channels = np.shape(tifimage)` When I load 4-band .tif image everything ok. When I load 1-band .tif image I get an error when I try to read `channels` variable…because `channels` variable gets nothing…How can I avoid that?

---

<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 30, 2021, 9:56pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/13 "2021-10-30T21:56:14Z")

</div>

```python
if len(tifimage.shape) == 2:
    (height, width) = tifimage.shape
    channels = 1
else:
    (height, width, channels) = tifimage.shape

```

---

<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 31, 2021, 12:43pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/15 "2021-10-31T12:43:55Z")

</div>

Hello! Thanks! I get this error, any ideas??

```auto
   if len(tifimage.shape) == 2:
AttributeError: 'NoneType' object has no attribute 'shape'

```

---

<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 31, 2021, 1:14pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/16 "2021-10-31T13:14:24Z")

</div>

[the image is empty. why is it empty?](https://www.youtube.com/watch?v=tkBVDh7my9Q)

---

<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 31, 2021, 1:24pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/17 "2021-10-31T13:24:31Z")

</div>

The image is not empty. I have just tested with QGIS and opens perfectly. However with the above code does not open, and when I check the shape (with another python code) it gives me only height and width and nothing/null for channels…

---

<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 31, 2021, 1:28pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/18 "2021-10-31T13:28:28Z")

</div>

> [@mike123](#):
>
> ```auto
> if len(tifimage.shape) == 2:
> AttributeError: 'NoneType' object has no attribute 'shape'
> 
> ```

that literally means `tifimage` was `None`

only you know why. I can’t see the code.

---

<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 31, 2021, 1:32pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/19 "2021-10-31T13:32:46Z")

</div>

This is the info of the .tif image I am trying to open:

`Band 1 Block=7421x1 Type=Float64, ColorInterp=Gray`

Do you see anything wrong?

---

<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 1, 2021, 6:58pm UTC](https://forum.opencv.org/t/how-can-i-get-the-bands-of-a-tiff-uav-image-using-tifffile/5880/20 "2021-11-01T18:58:28Z")

</div>

The .tif that I have the problem is `float64`. Has anything to do with this?
