plt.imread(img) raises PIL.UnidentifiedImageError

我的未来我决定 提交于 2021-01-28 05:12:20

问题


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

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.

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.

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

EDIT: this is the picture https://drive.google.com/file/d/1CjAGegeA9RUmTtgd_8xgrRPqOLx8NdMa/view?usp=sharing

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

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.


回答1:


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

tiffinfo v3l1_24-34.tiff

Or with ImageMagick:

magick identify -verbose v3l1_24-34.tiff

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

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

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

import tifffile
...
im = tiffffile.imread('v3l1_24-34.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

To check if a file is a BigTIFF or not, use exiftool and look for BTF like this:

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
...
...

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+.............
...
...

whereas a ClassicTIFF shows up as 0x2a:

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


来源:https://stackoverflow.com/questions/60658539/plt-imreadimg-raises-pil-unidentifiedimageerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!