Python PIL For循环使用多图像TIFF

时间:2022-03-07 00:23:52

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 [0,0] )and depending on what color it is in all 4 I will do something accordingly.

每个tiff文件中都有4个图像。如果可能的话,我不想提取并保存它们,我只想使用for循环查看它们中的每一个。 (就像看一下像素[0,0])并且根据它在所有4中的颜色,我会做相应的事情。

Is this possible using PIL? If not what should I use.

这可能使用PIL吗?如果不是我应该使用什么。

3 个解决方案

#1


13  

You can use the "seek" method of a PIL image to have access to the different pages of a tif (or frames of an animated gif).

您可以使用PIL图像的“搜索”方法来访问tif(或动画gif的帧)的不同页面。

from PIL import Image

img = Image.open('multipage.tif')

for i in range(4):
    try:
        img.seek(i)
        print img.getpixel( (0, 0))
    except EOFError:
        # Not enough frames in img
        break

#2


3  

Rather than looping until an EOFError, one can iterate over the image pages using PIL.ImageSequence (which effectively is equivalent as seen on the source code).

可以使用PIL.ImageSequence(实际上与源代码中看到的相同)迭代图像页面,而不是循环直到EOFError。

from PIL import Image, ImageSequence

im = Image.open("multipage.tif")

for i, page in enumerate(ImageSequence.Iterator(im)):
    page.save("page%d.png" % i)

#3


2  

Here's a method that reads a multipage tiff and returns the images as a numpy array

这是一个读取多页tiff并将图像作为numpy数组返回的方法

from PIL import Image
import numpy as np

def read_tiff(path, n_images):
    """
    path - Path to the multipage-tiff file
    n_images - Number of pages in the tiff file
    """
    img = Image.open(path)
    images = []
    for i in range(n_images):
        try:
            img.seek(i)
            slice_ = np.zeros((img.height, img.width))
            for j in range(slice_.shape[0]):
                for k in range(slice_.shape[1]):
                    slice_[j,k] = img.getpixel((j, k))

            images.append(slice_)

        except EOFError:
            # Not enough frames in img
            break

    return np.array(images)

#1


13  

You can use the "seek" method of a PIL image to have access to the different pages of a tif (or frames of an animated gif).

您可以使用PIL图像的“搜索”方法来访问tif(或动画gif的帧)的不同页面。

from PIL import Image

img = Image.open('multipage.tif')

for i in range(4):
    try:
        img.seek(i)
        print img.getpixel( (0, 0))
    except EOFError:
        # Not enough frames in img
        break

#2


3  

Rather than looping until an EOFError, one can iterate over the image pages using PIL.ImageSequence (which effectively is equivalent as seen on the source code).

可以使用PIL.ImageSequence(实际上与源代码中看到的相同)迭代图像页面,而不是循环直到EOFError。

from PIL import Image, ImageSequence

im = Image.open("multipage.tif")

for i, page in enumerate(ImageSequence.Iterator(im)):
    page.save("page%d.png" % i)

#3


2  

Here's a method that reads a multipage tiff and returns the images as a numpy array

这是一个读取多页tiff并将图像作为numpy数组返回的方法

from PIL import Image
import numpy as np

def read_tiff(path, n_images):
    """
    path - Path to the multipage-tiff file
    n_images - Number of pages in the tiff file
    """
    img = Image.open(path)
    images = []
    for i in range(n_images):
        try:
            img.seek(i)
            slice_ = np.zeros((img.height, img.width))
            for j in range(slice_.shape[0]):
                for k in range(slice_.shape[1]):
                    slice_[j,k] = img.getpixel((j, k))

            images.append(slice_)

        except EOFError:
            # Not enough frames in img
            break

    return np.array(images)