0

I am trying to visualize a tiff image in an ipython notebook using the following code, import statements are ignored for clarity purposes.

from PIL import Image
orig_img_path = os.path.join("path/to/tiff/0.tiff")
img_orig = Image.open(orig_img_path,"r")
plt.imshow(img_orig)
plt.show()

The above snippet just shows me the following black image -

enter image description here

I know that the image pixel values are non-zero since I can open the original images on my MacBook and they look like the following -

enter image description here

I also double check that the pixel values are nonzero by using the following code of casting the PIL image to an np array using the following code and printing the array out -

img_arr = np.array(img_orig)
print(img_arr)

which gives me the following output -

enter image description here

I think I know what the issue is as well - that the matplotlib pyplot thinks that this is a PNG image or something like that and as we can see the pixel values are very small to be perceivable by the human eyes. I mean I can simply multiply the image by 255 which gives the following image as expected, although scaling by 255 is not entirely right since the pixel values need to be normalized for the minimum to corrspond to 0 and max to 255 assuming mat plot lib thinks that it is showing a PNG image -

enter image description here

I would like to know if there is a better way of doing so rather than me manually doing it all the time, any native way of displaying tiff in a Jupyter/ipython notebook which preserves all the good properties of tiff images such as floating point numbers etc.

Thanks and please let me know if anything is unclear.

Edit 1: Link to the original file is here - https://drive.google.com/file/d/1O1-QM6aeU5-QZhT36vOMjDND2vkZNgqB/view?usp=sharing

-- Megh

Megh
  • 67
  • 1
  • 1
  • 7
  • 1
    What does `img_orig.show()` look like? Please upload the file in question so we can aid you in finding a solution – code-lukas Aug 24 '22 at 12:15
  • Hi, thanks a lot. I have added the google drive link to the original file in the original post. But, this is not the exact same file since I have deleted it, but it is of the same characteristics, hence this will be equally useful. Thank you again! – Megh Aug 28 '22 at 17:36
  • Does this answer your question? [How can I display an image from a file in Jupyter Notebook?](https://stackoverflow.com/questions/11854847/how-can-i-display-an-image-from-a-file-in-jupyter-notebook) – Jody Klymak Aug 28 '22 at 23:13

2 Answers2

2

If you share your original image in .tiff format the solution might be more precise.

You are reading the image pixels as type float: 0-1, and after that, you parse them as uint8 : 0-255 which will turn all pixels values into 0 or 1: Black or almost Black

You can try the following approach to read your image (supposedly Black and White) and parse it:

import cv2

gray = cv2.imread("path/to/tiff/0.tiff", cv2.IMREAD_UNCHANGED)

cv2.namedWindow("MyImage", cv2.WINDOW_NORMAL)
cv2.imshow("MyImage", gray)
cv2.waitKey(0)
HD123
  • 23
  • 2
  • Hello, thanks a lot. Please find the original file in the original post. It is not the exact same one but shares the same characteristics hence it is equally useful. Thank you again. – Megh Aug 28 '22 at 17:37
  • @Megh Please try the updated solution. – HD123 Aug 29 '22 at 18:45
0

What is the range of values that you expect in that image? Did you do any preprocessing to it?

The image you are trying to display in matplotlib contains negative values. Visualizing works well for any kind of uint8 data (Grayscale and RGB) and data in the range of 0 - 1. Your displaying issue can be addressed by adding the min value of the image and then dividing by the max value (effectively normalizing your data to the range 0-1).

[...]
img = np.array(img_orig, dtype=float)

img += abs(np.min(img))
img /= np.max(img)

plt.imshow(img)
plt.show()
code-lukas
  • 1,586
  • 9
  • 19