pythonopencvtiff

How to create multi-channel tif image with python


I have set of microscopy images, each subset has two channels which I need to merge into one .tif image (Each merged image should contain two channels)

import cv2 as cv

im1 = cv.imread('/img1.tif', cv.IMREAD_UNCHANGED)
im2 = cv.imread('/img1.tif', cv.IMREAD_UNCHANGED)

im3 = im1 + im2


cv.imwrite('img3.tif', im3)

That way it creates a file with one channel, but I'd like to get 16-bit .tif image with two channels that can be viewed separately in ImageJ, for example. Which way it can be corrected?


Solution

  • If you wanted to generate a TIFF file containing a "stack" of images, you don't need tifffile. You can use OpenCV's own function cv.imwritemulti():

    import cv2 as cv
    
    im1 = cv.imread('img1.tif', cv.IMREAD_UNCHANGED)
    im2 = cv.imread('img1.tif', cv.IMREAD_UNCHANGED)
    
    im3 = [im1, im2] # a stack of two
    
    cv.imwritemulti('img3.tif', im3)