python - 在 python 中使用 pil 读取 tif 图像时出现值错误?

标签 python python-imaging-library tiff

我必须读取大小为 2200x 2200 的 tif 图像并键入 uint16。我将 PIL 库与 anaconda python 一起使用,如下所示:

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

我遇到了以下错误:ValueError: tile cannot extend outside image

这可能是什么原因以及如何解决这个问题?我用的是anaconda python3.6.1版本

最佳答案

这是因为图像编码有错误; TIF 文件中的图 block 实际上确实延伸到了图像之外。您可以通过查看磁贴来确认这一点:

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')]

在我上面的示例中,图像尺寸为 1000x1000像素,但显然瓷砖延伸到 1200x1200 .您可以将图像裁剪到预期大小(丢失一些信息),或者扩大图像大小以包含所有图 block 。请参阅此处的示例:

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 - 在 python 中使用 pil 读取 tif 图像时出现值错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907780/

相关文章:

python - 最里面的括号叫什么?

python - 在 Python 中包含图片数据,可能吗?

java - 在不增加文件大小的情况下将 JPEG 图像转换为 TIFF

python - 你如何在 python 中读取 32 位 TIFF 图像?

python - 继承 `int` 的类获得奇怪的幽灵般的变量赋值

python - 将两个 df 和 tag 与列名匹配

python - 使用 python 发出 POST 请求失败,而使用 curl 发出相同请求成功。我错过了什么?

python - 使用 Python PIL 调整大小的图像较暗

c# - 从多个 BitmapImage 创建 Tiff 文件

python - 从文件中每 4 个字节读取 3 个字节到 bytearray