问题描述
我必须读取尺寸为 2200x 2200 的tif图像,然后键入uint16.我将PIL库与anaconda python结合使用,如下所示:
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()
我对此有以下错误:ValueError: tile cannot extend outside image
这可能是什么原因以及如何解决?我正在使用anaconda python3.6.1版本
What could be the reason for this and how to fix this? I am using anaconda python3.6.1 version
推荐答案
这是因为图像编码中存在错误; TIF文件中的图块实际上确实在图像外部延伸.您可以通过查看图块来确认这一点:
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
它将输出类似的内容:
[('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')]
[('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')]
在我上面的示例中,图像尺寸为1000x1000
像素,但很明显,图块扩展为1200x1200
.您可以将图像裁剪为期望的大小(丢失一些信息),或者扩展图像大小以包括所有图块.在此处查看示例:
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
即im.size = (1000, 1000)
或im.tile = [e for e in im.tile if e[1][2] < 1200 and e[1][3] < 1200]
这篇关于在python中使用pil读取tif图像时出现值错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!