Meaning Error while reading tif image with saw in python?
I need to read a 2200x 2200 tif image and type uint16. I am using PIL library with anaconda python like this:
from PIL import Image
img = Image.open('test.tif')
img.imshow()
I got the following error: ValueError: tile cannot extend outside image
What could be causing this and how to fix it? I am using anaconda python3.6.1 version
source to share
This is because there is a bug in the image encoding; the tiles in the TIF file do indeed propagate outside the image. You can confirm this by looking at the snippets:
img.tile
which will output something like:
[('tiff_lzw', (0, 0, 240, 240), 16, 'RGB'),
('tiff_lzw', (240, 0, 480, 240), 94905, 'RGB'),
...
('tiff_lzw', (720, 960, 960, 1200), 1711985, 'RGB'),
('tiff_lzw', (960, 960, 1200, 1200), 1730566, 'RGB')]
In the case of my example above, the image dimensions were 1000x1000
pixels, but obviously the tiles expand to 1200x1200
. You can either crop the image to the expected size (lose some information), or expand the image size to include all tiles. See examples here:
https://github.com/python-pillow/Pillow/issues/3044
ie, im.size = (1000, 1000)
orim.tile = [e for e in im.tile if e[1][2] < 1200 and e[1][3] < 1200]
source to share