Search code examples
pythonimagetypesscikit-imagerescale

skimage.transform.rescale changes datatype from uint8 to float64


When I use skimage.transform.rescale to reduce size of an image by 40% the datatype is converted from uint8 to float64.

I can manually change the datatype but I don't want to have too many manual interventions in my code.

Is there any rescaling method keeping the datatype? I am also alternatively open to methods changing the datatype.


Solution

  • You could use the utility function img_as_ubyte to convert the rescaled image back to uint8.

    Demo

    In [26]: from skimage import util, data, transform
    
    In [27]: img = data.moon()
    
    In [28]: img.shape
    Out[28]: (512, 512)
    
    In [29]: img.dtype
    Out[29]: dtype('uint8')
    
    In [30]: res = util.img_as_ubyte(transform.rescale(img, 0.6))
    
    In [31]: res.shape
    Out[31]: (307, 307)
    
    In [32]: res.dtype
    Out[32]: dtype('uint8')