Convert image from float64 to uint8 makes the image look darker

Maybe picture Maybe · Aug 23, 2019 · Viewed 8.9k times · Source

I have images of type float64 generated by GANs, and I save them through skimage.io.imsave. The process works well and the saved image looks nice, but I get a warning message as follows:

Lossy conversion from float64 to uint8. Range [-0.9999998807907104, 0.9999175071716309]. Convert image to uint8 prior to saving to suppress this warning.

Then I try to get rid of this warning by convert images to uint8 before saving using function skimage.img_as_ubyte. This gives me a apparently much darker image with a warning

UserWarning: Possible precision loss when converting from float64 to uint8 .format(dtypeobj_in, dtypeobj_out))

I've also tried to use other functions such as the one from tensorflow tf.image.convert_image_dtype before saving. They all return a darker image than I directly call skimage.io.imsave. What's the problem here?

Here's a set of images generated by converting to uint8 before saving enter image description here

Here's a set of images generated by saving directly

Answer

Cris Luengo picture Cris Luengo · Aug 23, 2019

From the documentation of skimage.img_as_ubyte that you linked:

Negative input values will be clipped. Positive values are scaled between 0 and 255.

Since your images are in the range [-1,1], half of the data will be set to 0, which is why stuff looks darker. Try first scaling your image to a positive-only range, for example by adding 1 to it, before calling skimage.img_as_ubyte.