如何使用多个通道调整 tiff 图像的大小?
How to resize a tiff image with multiple channels?
我有一张尺寸为 21 X 513 X 513 的 tiff 图像,其中 (513, 513) 是包含 21 个通道的图像的高度和宽度。如何将此图片调整为 21 X 500 X 375?
我正在尝试使用 PILLOW 来这样做。但是不知道是不是我做错了什么。
>>> from PIL import Image
>>> from tifffile import imread
>>> img = Image.open('new.tif')
>>> img
<PIL.TiffImagePlugin.TiffImageFile image mode=F size=513x513 at 0x7FB0C8E5B940>
>>> resized_img = img.resize((500, 375), Image.ANTIALIAS)
>>> resized_img
<PIL.Image.Image image mode=F size=500x375 at 0x7FB0C8E5B908>
>>> resized_img.save('temp.tif')
>>> img = imread('temp.tif')
>>> img.shape
(500, 375)
此处频道信息丢失。
您可以使用 OpenCV 调整图像大小。我能够使用以下代码调整 TIFF 格式图像的大小:
import cv2
file = "image.tiff"
img = cv2.imread(file)
print("original image size: ", img.shape)
new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100)) # cv2.resize(image,(width,height))
print("resized image size: ", new_img.shape)
输出:
原图尺寸:(512, 768, 3)
调整后的图片尺寸:(412, 668, 3)
Opencv 以INTER_LINEAR作为默认的插值方法。
您可以通过提供附加参数来更改插值
new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100),interpolation=cv2.INTER_AREA)
在此处阅读有关可用插值方法的更多信息:https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize
尝试使用 tifffile
和 scikit-image
:
from tifffile import imread, imwrite
from skimage.transform import resize
data = imread('2009_003961_SEG.tif')
resized_data = resize(data, (375, 500, 21))
imwrite('multi-channel_resized.tif', resized_data, planarconfig='CONTIG')
is not a multi-channel 513x513x21 image. Instead the file contains 513 images of size 513x21. The tifffile
库中链接的文件 2009_003961_SEG.tif
将读取文件中的一系列图像,并将其 return 作为形状为 513x513x21 的 numpy 数组。
要将 numpy 数组的大小调整为 375x500x21,请使用 skimage.transform.resize
(或 scipy.ndimage.zoom
)。单独调整 21 个通道的大小可能会更快。
要使用 tifffile
写入包含单个大小为 375x500x21 的多通道图像的 TIFF 文件,请指定 planarconfig
参数。没有多少库或应用程序可以处理此类文件。
我有一张尺寸为 21 X 513 X 513 的 tiff 图像,其中 (513, 513) 是包含 21 个通道的图像的高度和宽度。如何将此图片调整为 21 X 500 X 375?
我正在尝试使用 PILLOW 来这样做。但是不知道是不是我做错了什么。
>>> from PIL import Image
>>> from tifffile import imread
>>> img = Image.open('new.tif')
>>> img
<PIL.TiffImagePlugin.TiffImageFile image mode=F size=513x513 at 0x7FB0C8E5B940>
>>> resized_img = img.resize((500, 375), Image.ANTIALIAS)
>>> resized_img
<PIL.Image.Image image mode=F size=500x375 at 0x7FB0C8E5B908>
>>> resized_img.save('temp.tif')
>>> img = imread('temp.tif')
>>> img.shape
(500, 375)
此处频道信息丢失。
您可以使用 OpenCV 调整图像大小。我能够使用以下代码调整 TIFF 格式图像的大小:
import cv2
file = "image.tiff"
img = cv2.imread(file)
print("original image size: ", img.shape)
new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100)) # cv2.resize(image,(width,height))
print("resized image size: ", new_img.shape)
输出:
原图尺寸:(512, 768, 3)
调整后的图片尺寸:(412, 668, 3)
Opencv 以INTER_LINEAR作为默认的插值方法。
您可以通过提供附加参数来更改插值
new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100),interpolation=cv2.INTER_AREA)
在此处阅读有关可用插值方法的更多信息:https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize
尝试使用 tifffile
和 scikit-image
:
from tifffile import imread, imwrite
from skimage.transform import resize
data = imread('2009_003961_SEG.tif')
resized_data = resize(data, (375, 500, 21))
imwrite('multi-channel_resized.tif', resized_data, planarconfig='CONTIG')
tifffile
库中链接的文件 2009_003961_SEG.tif
将读取文件中的一系列图像,并将其 return 作为形状为 513x513x21 的 numpy 数组。
要将 numpy 数组的大小调整为 375x500x21,请使用 skimage.transform.resize
(或 scipy.ndimage.zoom
)。单独调整 21 个通道的大小可能会更快。
要使用 tifffile
写入包含单个大小为 375x500x21 的多通道图像的 TIFF 文件,请指定 planarconfig
参数。没有多少库或应用程序可以处理此类文件。