Question

I'm working on large satellite image files in the .tif format. To start, I am just trying to open the files and view them using PIL. Here is the code I have written so far:

from PIL import Image
import os.path

script_dir = os.path.dirname(os.path.abspath(__file__))
im = Image.open(os.path.join(script_dir, 'orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif'))
im.show()

Unfortunately, I am receiving the error message:

IOError                                   Traceback (most recent call last)
/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
      202             else:
      203                 filename = fname
----> 204             __builtin__.execfile(filename, *where)

/Users/zlazow/Desktop/Geo Research Files/documents-export-2014-02-13 (3)/showfiles.py in <module>()
      3 
      4 script_dir = os.path.dirname(os.path.abspath(__file__))
----> 5 im = Image.open(os.path.join(script_dir, 'orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif'))
      6 im.show()

/Users/zlazow/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/Image.pyc in open(fp, mode)
      1978                 pass
      1979 
----> 1980     raise IOError("cannot identify image file")
      1981 
      1982 #

IOError: cannot identify image file

Are the image files simply too large for PIL? I can open one of the smaller (200MB) .tif files in the Preview Application, but when I try to open it using PIL it creates a BMP image that opens in Preview, but the image never loads.

All of the rest of the files (300MB++) will not open with Preview or PIL at all.

Thanks for any assistance.

Was it helpful?

Solution

The Image constructor looks through its internal list of formats (depends on how PIL was compiled) and asks each one if it can parse the file.

As an input to the detector function, the first few bytes of the image file is used. By looking inside the TIFF image reader, it looks for one of the following magic bytes:

["MM\000\052", "II\052\000", "II\xBC\000"]

As indicated by the error message, the detector fails while reading the first few bytes of the file, way before it has gotten to read the dimensions of the image. One of the following reasons appear more likely:

  • The file is corrupt
  • The file is not a TIFF image
  • The file is some exotic/new TIFF sub-format that PIL can't understand

And as for solution, I would suggest:

  • Use the file command to try to identify the file format, e.g.

    file orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif

    which should print something like

    Untitled.tiff: TIFF image data, big-endian

  • Try to open the file in e.g. Photoshop and see if it can understand the file.

  • Inspect the header manually, see if the file starts with the magic bytes above.

EDIT: Since you identified the format (BigTIFF), you have two options: Convert it or find a Python library to load it. http://bigtiff.org has unofficial libtiff versions with BigTIFF built in. You could try to compile pylibtiff against this libtiff version, or use ImageMagick (compiled with BigTIFF support) to convert the images to regular TIFF files first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top