Vikas Bansal
Vikas Bansal

Reputation: 2930

what type of array is being returned by tiff.imread()?

I am trying to get the RGB value of pixels from the TIFF image. So, what I did is:

import tifffile as tiff 
a = tiff.imread("a.tif")
print (a.shape)    #returns (1295, 1364, 4)
print(a)      #returns  [[[205 269 172 264]...[230 357 304 515]][[206 270 174 270] ... [140 208 183 286]]]

But since we know pixel color ranges from (0,255) for RGB. So, I don't understand what are these array returning, as some values are bigger than 255 and why are there 4 values?

By the way array size is 1295*1364 i.e size of image.

Upvotes: 0

Views: 6418

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207758

The normal reasons for a TIFF (or any other image) to be 4-bands are that it is:

  • RGBA, i.e. it contains Red, Green and Blue channels plus an alpha/transparency channel, or
  • CMYK, i.e. it contains Cyan, Magenta, Yellow and Black channels - this is most common in the print industry where "separations" are used in 4-colour printing, see here, or
  • that it is multi-band imagery, such as satellite images with Red, Green, Blue and Near Infra-red bands, e.g. Landsat MSS (Multi Spectral Scanner) or somesuch.

Note that some folks use TIFF files for topographic information, bathymetric information, microscopy and other purposes.

The likely reason for the values to be greater than 256, is that it is 16-bit data. Though it could be 10-bit, 12-bit, 32-bit, floats, doubles or something else.

Without access to your image, it is not possible to say much more. With access to your image, you could use ImageMagick at the command-line to find out more:

magick identify -verbose YourImage.TIF

Sample Output

Image: YourImage.TIF
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 1024x768+0+0
  Units: PixelsPerInch
  Colorspace: CMYK           <--- check this field
  Type: ColorSeparation      <--- ... and this one
  Endianess: LSB
  Depth: 16-bit
  Channel depth:
    Cyan: 16-bit             <--- ... and this
    Magenta: 1-bit           <--- ... this
    Yellow: 16-bit           <--- ... and this
    Black: 16-bit
  Channel statistics:
    ...
    ...

You can scale the values like this:

from tifffile import imread
import numpy as np

# Open image 
img = imread('image.tif')

# Convert to numpy array
npimg = np.array(img,dtype=np.float)
npimg[:,:,0]/=256
npimg[:,:,1]/=256
npimg[:,:,2]/=256
npimg[:,:,3]/=65535

print(np.mean(npimg[:,:,0]))
print(np.mean(npimg[:,:,1]))
print(np.mean(npimg[:,:,2]))
print(np.mean(npimg[:,:,3]))

Upvotes: 1

Related Questions