I have a tiff image of size 21 X 513 X 513 where (513, 513) is the height and width of the image containing 21 channels. How can I resize this image to 21 X 500 X 375?
I am trying to use PILLOW to do so. But can't figure out if I am doing something wrong.
>>> 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)
The channel information is lost here.
Try using tifffile
and scikit-image
:
from tifffile import imread, imwrite
from skimage.transform import resizedata = imread('2009_003961_SEG.tif')
resized_data = resize(data, (375, 500, 21))
imwrite('multi-channel_resized.tif', resized_data, planarconfig='CONTIG')
The file 2009_003961_SEG.tif
linked in comment98601187_55975161 is not a multi-channel 513x513x21 image. Instead the file contains 513 images of size 513x21. The tifffile
library will read the series of images in the file and return it as a numpy array of shape 513x513x21.
To resize the numpy array to 375x500x21, use skimage.transform.resize
(or scipy.ndimage.zoom
). It might be faster to resize the 21 channels separately.
To write a TIFF file containing a single multi-channel image of size 375x500x21 using tifffile
, specify the planarconfig
parameter. Not many libraries or apps can handle such files.