stevosn
stevosn

Reputation: 483

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.

Upvotes: 8

Views: 16398

Answers (3)

John Walthour
John Walthour

Reputation: 826

This worked for me (the key flag being IMREAD_ANYDEPTH):

cv2.imread(fullImagePath, flags=(cv2.IMREAD_GRAYSCALE | cv2.IMREAD_ANYDEPTH))

I found it here: https://github.com/opencv/opencv/issues/7025

Upvotes: 5

Tonechas
Tonechas

Reputation: 13733

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

Upvotes: 11

stevosn
stevosn

Reputation: 483

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

Upvotes: 3

Related Questions