您可以创建一个包含12个图像的"multi-page" TIFF,每个图像的宽度为640px,高度为480px,如下所示:
from tifffile import imwrite
import numpy as np
# Create stack of 12 images, each 640x480 pixels
data = np.zeros((12,480,640), np.uint8)
# Make first image fully black, 2nd image grey(20), 3rd image grey(40)
for i in range(12):
data[i, :, :] = 20 * i
# Save as 12 image multi-page TIFF
imwrite('result.tif', data, photometric='minisblack')
如果您现在在macOS Preview图像查看器中打开它,您将看到单个TIFF文件包含12个图像,每个图像依次比前一个更亮:
如果你用ImageMagick判断它的内容,你会在单个TIFF中找到12个单独的图像:
identify result.tif
result.tif[0] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 3.51761MiB 0.000u 0:00.000
result.tif[1] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[2] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[3] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[4] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[5] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[6] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[7] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[8] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[9] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[10] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
result.tif[11] TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 0.000u 0:00.000
如果您现在想要将其读回,并将通道0、6和11作为R、G和B:
from tifffile import imread
from PIL import Image
im = imread('result.tif')
# Take channels 0, 6 and 11 as R, G and B
rgb = np.dstack((im[0], im[6], im[11]))
Image.fromarray(rgb).save('result.jpg')
红色通道是零,绿色通道是120,蓝色通道是220,所以结果基本上是蓝色的.