简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM