Ying Xiong
Ying Xiong

Reputation: 4928

imresize in PIL/scipy.misc only works for uint8 images? any alternatives?

It seems the imresize implemented in PIL/scipy.misc only works for uint8 images

>>> import scipy.misc
>>> im = np.random.rand(100,200)
>>> print im.dtype
float64

>>> im2 = scipy.misc.imresize(im, 0.5)
>>> print im2.dtype
uint8

Is there any way around this? I'd like to deal HDR images and therefore needs to deal with float64 or float32 images. Thanks.

Upvotes: 5

Views: 10026

Answers (3)

C.J
C.J

Reputation: 55

Talking about performance to complement Ying Xiong summary and based on arrays as Numpy.array whose datatype is either int or float, OpenCV is much faster:

import numpy as np
import cv2
from timeit import Timer
from scipy.ndimage import zoom


def speedtest(cmd, N):
    timer = Timer(cmd, globals=globals())
    times = np.array(timer.repeat(repeat=N, number=1))
    print(f'Over {N} attempts, execution took:\n'
          f'{1e3 * times.min():.2f}ms at min\n'
          f'{1e3 * times.max():.2f}ms at max\n'
          f'{1e3 * times.mean():.2f}ms on average')

# My image is 2000x2000, let's try to resize it to 300x300
image_int = np.array(image, dtype= 'uint8')
image_float = np.array(image, dtype= 'float')

N_attempts = 100  # We run the speed test 100 times
speedtest("zoom(image_int, 300/2000)", N_attempts)
# Over 100 attempts, execution took:
# 120.84ms at min
# 135.09ms at max
# 124.50ms on average
speedtest("zoom(image_float, 300/2000)", N_attempts)
# Over 100 attempts, execution took
# 165.34ms at min
# 180.82ms at max
# 169.77ms on average
speedtest("cv2.resize(image_int, (300, 300))", N_attempts)
# Over 100 attempts, execution took
# 0.11ms at min
# 0.26ms at max
# 0.13ms on average
speedtest("cv2.resize(image_float, (300, 300))", N_attempts)
# Over 100 attempts, execution took
# 0.56ms at min
# 0.86ms at max
# 0.62ms on average

Upvotes: 0

jgroehl
jgroehl

Reputation: 134

you could also use the mode='F' option in the imresize function

imresize(image, factor, mode='F')

Upvotes: 1

Ying Xiong
Ying Xiong

Reputation: 4928

Thanks to cgohlke's comment. Below are two alternatives I found that works for float-number images.

  1. Use scipy.ndimage.interpolation.zoom

For single-channel images: im2 = scipy.ndimage.interpolation.zoom(im, 0.5)

For 3-channel images: im2 = scipy.ndimage.interpolation.zoom(im, (0.5, 0.5, 1.0))

  1. Use OpenCV.

im2 = cv2.resize(im, (im.shape[1]/2, im.shape[0]/2))

This works for both single-channel and 3-channel images. Note that one needs to revert the shape order in second parameter.

Upvotes: 12

Related Questions