1

I needed a routine to read a tiff using python. I used the suggestiom by Michael Brennan at Working with TIFFs (import, export) in Python using numpy

import matplotlib
import matplotlib.pyplot as plt
I = plt.imread('ASTGTM_N26E093_num.tif')
print I

I get a runtime error : Unknown Image mode

File "img1.py", line 10, in <module>                                             
I = plt.imread('ASTGTM_N26E093_num.tif')                                      
File "/usr/lib64/python2.6/site-packages/matplotlib/image.py", line 776, in imread
im = pilread()                                                                
File "/usr/lib64/python2.6/site-packages/matplotlib/image.py", line 767, in pilread                                                                                       
return pil_to_array(image)
File "/usr/lib64/python2.6/site-packages/matplotlib/image.py", line 853, in pil_to_array                                                                           
raise RuntimeError('Unknown image mode')
RuntimeError: Unknown image mode     

Any suggestions??

Community
  • 1
  • 1

2 Answers2

0

Matplotlib mentioned PIL as requirement for reading several image types. Maybe you could try to install PIL and test your code again.

http://matplotlib.org/api/pyplot_api.html?highlight=imread#matplotlib.pyplot.imread

Kuishi
  • 157
  • 4
  • I checked for PIL and it was already installed. But I still get the same error. – Ranjani R. Varun Feb 12 '14 at 06:46
  • you have to reinstall PIL with tiff support. to have PIL installed with tiff support you have to install libtiff-dev and reinstall PIL. Also try to save the file with "tiff" instead of "tif" – llazzaro Jul 27 '16 at 20:16
0

Try pylibtiff, it work for me with a stack of tiff images where matplotlib or PIL fail. From their help:

from libtiff import TIFF
# to open a tiff file for reading:
tif = TIFF.open('filename.tif', mode='r')
# to read an image in the currect TIFF directory and return it as numpy array:
image = tif.read_image()
# to read all images in a TIFF file:
for image in tif.iter_images(): # do stuff with image
# to open a tiff file for writing:
tif = TIFF.open('filename.tif', mode='w')
# to write a image to tiff file
tif.write_image(image) 
scrx2
  • 2,242
  • 1
  • 12
  • 17
  • I have a stack of tiff and both PIL and matplotlib only import a single image from the stack. >> tif = TIFF.open('filename.tif', mode='r') >> image = tif.read_image() raise NotImplementedError("bits = %d" % bits) NotImplementedError: bits = 12 I see lots if "todo" comments in the pylibtiff source so am wondering whether it is still in development. – Frikster Jan 26 '16 at 20:20