Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 600
Ying Xiong Avatar asked Nov 07 '14 03:11

Ying Xiong


2 Answers

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.

like image 132
Ying Xiong Avatar answered Oct 13 '22 00:10

Ying Xiong


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

imresize(image, factor, mode='F')
like image 21
jgroehl Avatar answered Oct 12 '22 23:10

jgroehl