Can I load a multi-frame TIFF through OpenCV?

Unfortunately OpenCV does not support TIFF directories and is able to read only the first frame from multi-frame TIFF files.


While OpenCV can't open multi-frame TIFF files, you can open the image using PIL and then pass the data on to OpenCV. I haven't yet been able to get it working with the new "cv2" namespace

tiff = Image.open('sample.tif')
try:
    while 1:
        # Convert PIL image to OpenCV
        image = cv.CreateImageHeader(tiff.size, cv.IPL_DEPTH_8U, 1)
        cv.SetData(image, tiff.tostring()) # It's "tostring" and not "toString()"!
        # Do whatever you're going to do with OpenCV data
        tiff.seek(tiff.tell()+1)
except EOFError:
    pass

OpenCV is now capable of reading a multi-page TIFF using the imreadmulti function. See this page from the OpenCV 3.4 documentation:

https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#ga4dd47c9ae3d55cc42286cff005825e31


You can load multi-frame tiff files in OpenCV using file read function called imreadmulti. Here is the example

 ret, images = cv2.imreadmulti('<path_of_tiff_files>.tiff', [], cv2.IMREAD_ANYCOLOR)

Images will be a list of frames in the tiff file. Suppose you want to see 2nd image, you can access as

img = images[1] # note 0 based indexing