3

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I get this in return: orthoQB02_11JUL040015472-M1BS-101001000DB70900_NDVI.tif: TIFF image data, little-endian
And this II+J‡Ì7âbó=BCDQëEQúSÉ as the file header (seen from TextEdit)
@Plazsma it would have been better to get that header in binary rather than text, but I think I see the problem - the TIFF version is listed as + which is \053 and not \052. That makes it non-standard. Edit: It's probably BigTIFF, see remotesensing.org/libtiff/bigtiffdesign.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.