1

I have a tif images, its type is float32, shape is (128*128) (a grayscale image). All the pixel values are of range [0.0, 1.0]

When I read it using skimage and output it using matplotlib. The output image looks like a RGB image.

enter image description here

from skimage import io
import matplotlib.pyplot as plt
output=io.imread(os.path.join(image_path,raw_image_name))
print(output.dtype)
print(output.shape)
print(output.max())
print(output.min())
plt.imshow(output)
plt.show()

However, when I read the image using matplotlib instead, output=plt.imread(os.path.join(image_path,raw_image_name)). I found that pixel value will become 255 and 0. The output image will become black as shown in the second image. I am confused how does this work? My guess is that there are some dtype change happening during the reading process.

enter image description here

user297850
  • 7,705
  • 17
  • 54
  • 76

1 Answers1

1

When you display images read with skimage with matplotlib just give it cmap='gray' like so:

plt.imshow(output,cmap='gray')

This should give you a grayscale image of your data. Also check out this answer.

When you read in tif images using Matplotlib it will try to convert things to a range 0...255, because it falls back to PIL to handle tiffs, which only works with uint8 values according to the docs:

As a side note, the only datatype that PIL can work with is uint8. Matplotlib plotting can handle float32 and uint8, but image reading/writing for any format other than PNG is limited to uint8 data.

Community
  • 1
  • 1
lhcgeneva
  • 1,981
  • 2
  • 21
  • 29
  • Thanks. So if an image was saved as tif, or any other files with types instead of unit8, it is not right to read it using matplotlib. Is my understanding correct? – user297850 Dec 11 '16 at 19:31
  • I think most tif images will be saved as uint8 or similar, at least when it comes to microscopy imaging data I don't think I've every encountered a tif file with values between 0 and 1. So yes, if you would like to read in data that's not uint8 don't use matplotlib, I'm not sure how it performs on uint16/uint32. – lhcgeneva Dec 11 '16 at 20:08