Save numpy array as image with high precision (16 bits) with scikit-image

前端 未结 1 478
死守一世寂寞
死守一世寂寞 2020-12-10 04:51

I am working with 2D floating-point numpy arrays that I would like to save to greyscale .png files with high precision (e.g. 16 bits). I would like to do this using the scik

相关标签:
1条回答
  • 2020-12-10 04:53

    You wanna use the freeimage library to do so:

    import numpy as np
    from skimage import io, exposure, img_as_uint, img_as_float
    
    io.use_plugin('freeimage')
    
    im = np.array([[1., 2.], [3., 4.]], dtype='float64')
    im = exposure.rescale_intensity(im, out_range='float')
    im = img_as_uint(im)
    
    io.imsave('test_16bit.png', im)
    im2 = io.imread('test_16bit.png')
    

    Result:

    [[    0 21845]
     [43690 65535]]
    

    As for 3D arrays, you need to construct the array properly and then it'll work:

    # im = np.array([[1, 2.], [3., 4.]], dtype='float64')
    im = np.linspace(0, 1., 300).reshape(10, 10, 3)
    im = exposure.rescale_intensity(im, out_range='float')
    im = img_as_uint(im)
    
    io.imsave('test_16bit.png', im)
    im2 = io.imread('test_16bit.png')
    

    Note that the read image is flipped, so something like np.fliplr(np.flipud(im2)) will bring it to original shape.

    0 讨论(0)
提交回复
热议问题