plt.imread(img) 引发 PIL.UnidentifiedImageError [英] plt.imread(img) raises PIL.UnidentifiedImageError

查看:335
本文介绍了plt.imread(img) 引发 PIL.UnidentifiedImageError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我尝试在 TIFF 文件上使用 imread 时,会引发此错误.

So when I try to use imread on a TIFF-file, it raises this error.

有问题的TIFF文件是我在另一个脚本中创建和修改的TIFF.我最初是这样创建的: temp = np.full([max_y,max_x],np.nan).然后我循环单元格并更改单元格值.之后我使用 cv2.imwrite 将其写入文件.

The TIFF-file in question is a TIFF I created and modified in another script. I initially created it like this: temp = np.full([max_y, max_x], np.nan). Then I looped the cells and changed the cell values. Afterwards I used cv2.imwrite to write it to a file.

但是,我在其中创建和修改TIFF文件的原始脚本也会在写入文件后打开它,因此不知道为什么随后会打开它,而不是在此新脚本中不会打开.从那以后我所做的唯一更改是复制文件.

However the original script where I create and modify the TIFF-file also opens it after writing the file so no clue why it opens then, and not in this new script. The only change I've made since then is copying the file.

我也可以在 QGIS 中打开文件,所以不知道问题出在哪里.

I can also open the file in QGIS so no clue what the problem is.

这是图片https://drive.google.com/file/d/1CjAGegeA9RUmTtgd_8xgrRPqOLx8NdMa/view?usp=sharing

您可以在此处找到代码的相关部分: https://codeshare.io/5oYNDo .该错误发生在第32行.

The relevant part of the code you can find here: https://codeshare.io/5oYNDo. The error occurs in line 32.

编辑 2: 在 Mark 的帮助下,我已将其转换为 32 位图像: https://imgur.com/a/jbppYuO )由于NaN的vmin和vmax无法正常工作,这就是为什么整个图片呈黄色的原因.如果我删除了vmin和vmax,它会可视化.但是充满NaN的数组将在脚本中进一步导致问题,因此必须首先解决.

EDIT 2: With the help of Mark I've got it transformed to a 32-bit image: https://drive.google.com/file/d/1xDyqeEzMrB-84ms9BB7YztbT-_kfJpeG/view?usp=sharing. However plt.imread still didn't work. So now I am using img_arr = np.asarray(Image.open(img)). However this results in a strange bug: the array is full of NaN's but when I hover over a picture I do get the cell value. (https://imgur.com/a/jbppYuO) Because of the array full of NaN's vmin and vmax are not working properly, that's why the whole picture is yellow. If I remove the vmin and vmax it's visualized as it should. But the array full of NaN's will result in problems further in the script so this has to be solved first.

推荐答案

您的图像是 64 位浮点 TIFF.你可以看到:

Your image is a 64-bit floating point TIFF. You can see that with:

tiffinfo v3l1_24-34.tiff

或使用 ImageMagick:

magick identify -verbose v3l1_24-34.tiff

PIL 不喜欢这样的事情,因此您要么需要将其创建为32位:

PIL doesn't like such things, so you either need to either create it as 32-bit:

temp = np.full(..., dtype=np.float32)

或者,如果您需要 64 位,可以使用 tifffile 阅读:

or, if you need 64-bit, maybe read it with tifffile:

import tifffile
...
im = tiffffile.imread('v3l1_24-34.tiff')

<小时>

如果您想要将一些预先存在的 BigTIFF 文件制作成 32 位经典 TIFF 文件,您可以尝试以下命令:


If you have some pre-existing BigTIFF files you want to make into 32-bit classic TIFF files, you can try the following commands:

# Use an ImageMagick version with Q16 or higher and HDRI when you run "identify -version"
magick input.tif -define quantum:format=floating-point -depth 32 output.tif

# Or use "libvips" in the Terminal
vips im_vips2tiff input.tif output.tif

<小时>

要检查文件是否为BigTIFF,请使用 exiftool 并查找 BTF ,如下所示:

exiftool bigtiff.tif 
ExifTool Version Number         : 11.11
File Name                       : bigtiff.tif
Directory                       : .
File Size                       : 1024 kB
File Modification Date/Time     : 2020:03:13 13:56:05+00:00
File Access Date/Time           : 2020:03:13 13:56:19+00:00
File Inode Change Date/Time     : 2020:03:13 13:56:11+00:00
File Permissions                : rw-r--r--
File Type                       : BTF           <--- HERE
...
...

或者像这样使用 xxd 并查找第三个字节是 0x2b:

Or use xxd like this and look for 3rd byte being 0x2b:

xxd bigtiff.tif | more
00000000: 4949 2b00 0800 0000 1000 1000 0000 0000  II+.............
...
...

而 ClassicTIFF 显示为 0x2a:

whereas a ClassicTIFF shows up as 0x2a:

xxd classic.tiff | more
00000000: 4949 2a00 0800 1000 0000 803f 0000 803f  II*........?...?

这篇关于plt.imread(img) 引发 PIL.UnidentifiedImageError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆