Pillow: ValueError: tile cannot extend outside image

Created on 16 Mar 2018  路  5Comments  路  Source: python-pillow/Pillow

What did you do?

I'm attempting to load a satellite image from Planet.com by running:
image = Image.open(image_file_name)
pix = image.load()
When my code attempts to run image.load() I receive the above referenced error

What did you expect to happen?

I'm expected the tif image to load to be parsed for vectors

What actually happened?

I receive the error: ValueError: tile cannot extend outside image

What versions of Pillow and Python are you using?

Python 3.6.3 :: Anaconda, Inc
Pillow: 1.1.7

Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, Django, or buildout, try to replicate the issue just using Pillow.
PlanetImage.tif.zip
PlanetImage.tif.zip

>>> from PIL import Image, ImageDraw
>>> image_file_name=('PlanetImage.tif')
>>> image = Image.open(image_file_name)
>>> pix = image.load()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jwfowler/anaconda3/envs/ship_classify/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1053, in load
    return super(TiffImageFile, self).load()
  File "/Users/jwfowler/anaconda3/envs/ship_classify/lib/python3.6/site-packages/PIL/ImageFile.py", line 206, in load
    decoder.setimage(self.im, extents)
ValueError: tile cannot extend outside image

Bug

Most helpful comment

The tiff that you're loading has tiles that extend outside the image boundaries. Basically, the reported size of the image is wrong with respect to the encoded data.

There are two things that can be done, either set the image size to the maximum tile extents, or strip out the tile elements that are outside the reported image size:

>>> im = Image.open('issue_3044.tif')
>>> im.tile
[('raw', (0, 0, 128, 128), 2829, ('RGB', 0, 1)), 
('raw', (128, 0, 256, 128), 51981, ('RGB', 0, 1)), 
...
('raw', (2048, 1280, 2176, 1408), 9636621, ('RGB', 0, 1)), 
('raw', (2176, 1280, 2304, 1408), 9685773, ('RGB', 0, 1))]
>>> im.size = (2304, 1408)
>>> im.show()

or

>>> im = Image.open('issue_3044.tif')
>>> im.tile = [e for e in im.tile if e[1][2] < 2181 and e[1][3]<1294]
>>> im.show()

All 5 comments

The tiff that you're loading has tiles that extend outside the image boundaries. Basically, the reported size of the image is wrong with respect to the encoded data.

There are two things that can be done, either set the image size to the maximum tile extents, or strip out the tile elements that are outside the reported image size:

>>> im = Image.open('issue_3044.tif')
>>> im.tile
[('raw', (0, 0, 128, 128), 2829, ('RGB', 0, 1)), 
('raw', (128, 0, 256, 128), 51981, ('RGB', 0, 1)), 
...
('raw', (2048, 1280, 2176, 1408), 9636621, ('RGB', 0, 1)), 
('raw', (2176, 1280, 2304, 1408), 9685773, ('RGB', 0, 1))]
>>> im.size = (2304, 1408)
>>> im.show()

or

>>> im = Image.open('issue_3044.tif')
>>> im.tile = [e for e in im.tile if e[1][2] < 2181 and e[1][3]<1294]
>>> im.show()

Thanks! That solved it. It also worked when the image was converted to a .png. Does pillow handle .png's differently than .tiff files?

The tiff that you're loading has tiles that extend outside the image boundaries. Basically, the reported size of the image is wrong with respect to the encoded data.

FWIW, tiles extending outside the image conform to the TIFF 6.0 specification:

Boundary tiles are padded to the tile boundaries. For example, if TileWidth is 64
and ImageWidth is 129, then the image is 3 tiles wide and 63 pixels of padding
must be added to fill the rightmost column of tiles. The same holds for TileLength
and ImageLength. It doesn鈥檛 matter what value is used for padding, because good
TIFF readers display only the pixels defined by ImageWidth and ImageLength
and ignore any padded pixels.

Same for this gif image: a28ef5716b5d3f5813d8d08df6c29740f093a635.gif.zip

Looks like the tile is incorrect, reported image.size is (250, 289)
im.tile reports (24, 21, 274, 310)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SysoevDV picture SysoevDV  路  3Comments

anonymous530 picture anonymous530  路  3Comments

readyready15728 picture readyready15728  路  4Comments

nomarek picture nomarek  路  3Comments

Larivact picture Larivact  路  4Comments