[scikit-image] how to correctly save an numpy array with float32 type into an image

wine lover winecoding at gmail.com
Wed Dec 7 18:50:40 EST 2016


Hi Juan, Hi Imanol,


Thank you so much for your replies, which are very helpful.


During saving the image, I got some warning messages such as follows. What
does it indicate?

/development/lib/python3.4/site-packages/scikit_image-0.12.3-py3.4-linux-x86_64.egg/skimage/io/_io.py:132:
UserWarning: /data/train/image_sampled.tif is a low contrast
image

  warn('%s is a low contrast image' % fname)


Thanks,

Yuanyuan

On Wed, Dec 7, 2016 at 4:54 AM, Imanol Luengo <
imanol.luengo at nottingham.ac.uk> wrote:

> Hello,
>
> I would say there are two differences between 'Saving the data' and
> 'Displaying the data'. An image is discretized to `uint8` or `uint16` prior
> to being saved as standard formates (`.png` or `.jpg`). You could do
> something like
> ```
> import numpy as np
> from skimage import io, util
>
> A = np.random.rand(100,100)
> io.imsave('tmp.png', A)
> B = util.img_as_float(io.imread('tmp.png')
>
> assert np.allclose(A, B) # ERROR
> ```
>
> But you will find some discretization errors, which makes `B != A`. Having
> said that, if you want to preserve the data in `B`, I think the best option
> is to export the data using another format, e.g. numpy arrays:
> ```
> import numpy as np
>
> A = np.random.rand(100,100)
> np.save('tmp.npy', A)
> B = np.load('tmp.npy')
>
> assert np.allclose(A, B) # True
> ```
>
> Or alternatively, if you really want to save the data in a visualizable
> format, exporting the image as `.tif` format, which preserves data
> information, should also work:
> ```
> A = np.random.rand(100,100)
> io.imsave('tmp.tif', A)
> B = io.imread('tmp.tif')
>
> assert np.allclose(A, B) # True
> ```
>
> However, I would personally store my data in non-visualizable formats such
> as `.npy, .h5` (the later if you work with tons of data) as they usually
> offer another advantages (e.g. Datasets in HDF5).
>
> Hope it helps,
>
> Imanol
>
> On 07/12/16 04:41, wine lover wrote:
>
> Dear All,
>
>
> In a program, I generate an numpy array, with shape (128,128), which is
> supposed to represent an image.
>
> For instance, I have an array temp_mask, which is of type float32 and
> shape (128,128), the maximum value is 1.0 and the minimum value is 0.0.  I
> saved it using io.imsave(‘mask_image’,temp_mask) However, after I
> re-opened this image using img_mask = io.imread(‘mask_image’). The read
> image turns out to  have type unit16, the max value becomes 65535 and the
> min value is 0 .  It seems to me that io.imsave automatically transform
> the float32 array into an unit16 array.
>
> Is it possible to save the image while keeping the original type? If not,
> what’s the correct way to save an image represented as an array, with type
>  float32 and the range of value [0.0,1.0]?
>
>
> Thank you very much!
>
>
> _______________________________________________
> scikit-image mailing listscikit-image at python.orghttps://mail.python.org/mailman/listinfo/scikit-image
>
>
>
>
> This message and any attachment are intended solely for the addressee
> and may contain confidential information. If you have received this
> message in error, please send it back to me, and immediately delete it.
>
> Please do not use, copy or disclose the information contained in this
> message or in any attachment.  Any views or opinions expressed by the
> author of this email do not necessarily reflect the views of the
> University of Nottingham.
>
> This message has been checked for viruses but the contents of an
> attachment may still contain software viruses which could damage your
> computer system, you are advised to perform your own checks. Email
> communications with the University of Nottingham may be monitored as
> permitted by UK legislation.
>
>
> _______________________________________________
> scikit-image mailing list
> scikit-image at python.org
> https://mail.python.org/mailman/listinfo/scikit-image
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20161207/34e8bd9d/attachment.html>


More information about the scikit-image mailing list