Value Error in reading tif image with pil in pytho

2019-07-07 14:52发布

I have to read a tif image of size 2200x 2200 and type uint16. I use PIL library with anaconda python as follows:

from PIL import Image
img = Image.open('test.tif')
img.imshow()

I got following error with this: ValueError: tile cannot extend outside image

What could be the reason for this and how to fix this? I am using anaconda python3.6.1 version

2条回答
三岁会撩人
2楼-- · 2019-07-07 15:06

This is because there is an error in the image encoding; the tiles in the TIF file actually do extend outside the image. You can confirm this by viewing the tiles:

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 clearly the tiles extend to 1200x1200. You can either crop the image to the expected size (losing some information), or expand the image size to include all the tiles. See examples here:

https://github.com/python-pillow/Pillow/issues/3044

i.e., im.size = (1000, 1000) or im.tile = [e for e in im.tile if e[1][2] < 1200 and e[1][3] < 1200]

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-07 15:28

The problem is that PIL wants to see a ".tiff" at the end of the file name. You have ".tif". The solution is to rename your file to "test.tiff".

查看更多
登录 后发表回答