8

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

Maybe
  • 2,129
  • 5
  • 25
  • 45

2 Answers2

10

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
8

I fix this warning by using,

import numpy as np
import imageio

# suppose that img's dtype is 'float64'
img_uint8 = img.astype(np.uint8)
# and then
imageio.imwrite('filename.jpg', img_uint8)

That's it!

Anh-Thi DINH
  • 1,845
  • 1
  • 23
  • 17