Python PIL For Loop to work with Multi-image TIFF

前端 未结 5 479
清酒与你
清酒与你 2020-12-09 05:33

Each tiff file has 4 images in it. I do not wish to extract and save them if possible, I would just like to use a for loop to look at each of them. (Like look at the pixel [

5条回答
  •  囚心锁ツ
    2020-12-09 05:42

    Had to do the same thing today,

    I followed @stochastic_zeitgeist's code, with an improvement (don't do manual loop to read per-pixel) to speed thing up.

    from PIL import Image
    import numpy as np
    
    def read_tiff(path):
        """
        path - Path to the multipage-tiff file
        """
        img = Image.open(path)
        images = []
        for i in range(img.n_frames):
            img.seek(i)
            images.append(np.array(img))
        return np.array(images)
    

提交回复
热议问题