Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read a 32-bit TIFF image in python?

I want to read 32-bit float image files with python to do some image analysis.

I have tried

import matplotlib.pyplot as plt

im = plt.imread('path_to_file.tif')

But, this only reads the data as 8-bit integer values. Is there a way to provide imread() with the right data type?

-- Well, it formerly worked out of the box with 16-bit TIFF files, but doesn't with 32-bit floats.

like image 217
stevosn Avatar asked Dec 24 '22 23:12

stevosn


2 Answers

I experienced a similar problem trying to read single-channel 32-bit integer images. The solution I came up with was:

from skimage import io
im = io.imread('path_to_file.tif')

If you have OpenCV installed on your computer you could also try:

import cv2
im = cv2.imread('path_to_file.tif', -1)

Hope this helps

like image 176
Tonechas Avatar answered Jan 08 '23 15:01

Tonechas


I found a way via PIL, which is:

from matplotlib import pyplot as plt
from matplotlib import cm

from PIL import Image
from numpy import array

im = Image.open('path_to_file.tif')

ncols, nrows = im.size
ima = array(im.getdata()).reshape((nrows, ncols))
plt.imshow(ima, cmap=cm.Greys_r)

May be that helps someone.

S

like image 35
stevosn Avatar answered Jan 08 '23 16:01

stevosn