Python Pil Struggles With Uncompressed 16-bit Tiff Images
My system is Mac OS X v10.8.2. I have several 2560x500 uncompressed 16-bit TIFF images (grayscale, unsigned 16-bit integers). I first attempt to load them using PIL (installed vi
Solution 1:
It turns out that Matplotlib handles 16-bit uncompressed TIFF images in two lines of code:
import matplotlib.pyplot as plt
img = plt.imread(filename)
# >>> img# array([[38948, 41457, 37714, ..., 61511, 61785, 61824],# [39704, 38083, 36690, ..., 61419, 60086, 61910],# [41449, 39169, 38178, ..., 60192, 60969, 63538],# ...,# [37963, 39531, 40339, ..., 62351, 62646, 61793],# [37462, 37409, 38370, ..., 61125, 62497, 59770],# [39753, 36905, 38778, ..., 61922, 59565, 60035]], dtype=uint16)
Et voila. I suppose this doesn't meet my requirements as "lightweight" since Matplotlib is (to me) a heavy module, but it is spectacularly simple to get the image into a Numpy array. I hope this helps someone else find a solution quickly as this wasn't obvious to me.
Solution 2:
Try Pillow, the “friendly” PIL fork. They've somewhat recently added better support for 16- and 32-bit images including in the numpy array interface. This code will work with the latest Pillow:
from PIL import Image
import numpy as np
img = Image.open('data.tif')
data = np.array(img)
Post a Comment for "Python Pil Struggles With Uncompressed 16-bit Tiff Images"