6

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.

Damanpreet kaur
  • 123
  • 3
  • 16

2 Answers2

6

Try using tifffile and 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')

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.

cgohlke
  • 9,142
  • 2
  • 33
  • 36
1

You can use OpenCV to resize your image. I was able to resize a TIFF format image using following code:

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)

Output:
original image size: (512, 768, 3)
resized image size: (412, 668, 3)

Opencv takes INTER_LINEAR as default interpolation method.

You can change interpolation by providing an additional argument

new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100),interpolation=cv2.INTER_AREA)

Read more about available interpolation method here: https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize

Deep
  • 616
  • 1
  • 9
  • 17
  • Tried using opencv. But it gives me this error: cv2.imread('new.tif') [ WARN:0] OpenCV TIFF: TIFFRGBAImageOK: Sorry, can not handle images with 32-bit samples – Damanpreet kaur May 03 '19 at 18:16
  • Can you comment your opencv code? also can you provide your original image? I will try to run it on my end. – Deep May 03 '19 at 18:19
  • Here's the image: https://drive.google.com/file/d/1695sC1AvQQDQ8k02Phog0YSg1rP_7xIz/view?usp=sharing – Damanpreet kaur May 03 '19 at 18:24
  • My open cv code is just two lines. It fails while trying to read the file. I guess, it's because of the number of channels in my image (i.e. 21 instead of 3). >>> import cv2 >>> cv2.imread('2009_003961_SEG.tif') [ WARN:0] OpenCV TIFF: TIFFRGBAImageOK: Sorry, can not handle images with 32-bit samples – Damanpreet kaur May 03 '19 at 18:26
  • It looks like it maybe the problem with the opencv version. Version below 3.4.5 would give some sort of error. Try to upgrade it and try. – Deep May 03 '19 at 18:41
  • I am using opencv version is '4.1.0'. Still doesn't work. I have done a workaround for now by downsampling each channel separately and then stacking them together. – Damanpreet kaur May 03 '19 at 22:00