从单个 RBG 图片开始创建 12 个多通道图像

问题描述 投票:0回答:1

我有 12 个三通道 RGB 图片 (.jpeg)。我想使用 python 将它们合并到一个独特的 12 通道图像中。

我尝试使用以下代码将每张图片转换为 numpy 数组。我成功创建了一个在轴 2 上有 12 个数组的 3D 数组,但 PIL 似乎不支持保存多通道图像:

from PIL import Image
import numpy as np

# Load the first image
path = 'test_img_0000_Ch1.jpeg'
img = Image.open(path)
arr = np.array(img.convert('L'))

for x in range(2, 13):
    path = '_test_img_0000_Ch{x}.jpeg'
    img = Image.open(path)
    array_stack = np.array(img.convert('L'))
    arr = np.dstack((arr, array_stack))

image = Image.fromarray(arr)
image.save("multichannel.jpeg")
python numpy image-processing python-imaging-library
1个回答
0
投票

您可以创建一个 “多页” TIFF,其中包含 12 个图像,每个图像宽 640 像素,高 480 像素,如下所示:

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
© www.soinside.com 2019 - 2024. All rights reserved.