问题:使用 PIL.Image.open() 能正常打开大部分图像文件,但是webp格式的图像无法打开,有一些jpg,png图像也无法打开,报错:
PIL.UnidentifiedImageError: cannot identify image file xxx。
测试发现,这些图像在windows环境下使用图片查看器能正常查看,说明不是image file本身的问题。
from PIL import Image
# im_path = './test.webp'
im_path = './test.jpg'
im = Image.open(im_path, 'r')
print(im)
同时,上述代码能够打开其他正常的图像,说明代码没有问题。
可能是pillow包的问题导致图像无法打开,在安装pillow包的时候没有webp的支持,导致无法读取webp文件。
可以通过下面的方式查看本机已安装的pillow是否有webp的支持:
from PIL import features
print(features.pilinfo()) # info of pillow
# print(features.check_module('webp')) # if webp support, True or False
从下面这段代码的输出结果(红色字体部分)可以看出,本机安装的pillow没有webp支持:
--------------------------------------------------------------------
Pillow 8.0.1
Python 3.7.9 (default, Aug 31 2020, 12:42:55)
[GCC 7.3.0]
--------------------------------------------------------------------
Python modules loaded from /home/xxx/python3.7/site-packages/PIL
Binary modules loaded from /home/xxx/python3.7/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 8.0.1
--- TKINTER support ok
--- FREETYPE2 support ok, loaded 2.10.4
--- LITTLECMS2 support ok, loaded 2.10
*** WEBP support not installed
*** WEBP Transparency support not installed
*** WEBPMUX support not installed
*** WEBP Animation support not installed
--- JPEG support ok, compiled for 9.0
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
--- LIBTIFF support ok, loaded 4.1.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
.........................这里省略..............................................
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------
None
首先要为本机安装webp的支持,然后重新安装pillow包。
对于unix系的电脑,输入以下命令安装webp支持:
sudo apt-get install libwebp-dev
对于maxos,命令如下:
brew install webp
然后需要重新安装anaconda中pillow包。为了保险,建议读者最好新建一个anaconda虚拟环境,然后在新建的环境中对包进行卸载和重装,避免重装过程中出现问题导致现有环境出错,那就哭都没地方哭了。。。。。。
conda uninstall pillow
conda install pillow
然后测试,发现pillow已经有webp的支持了。
--------------------------------------------------------------------
Pillow 9.4.0
Python 3.7.9 (default, Aug 31 2020, 12:42:55)
[GCC 7.3.0]
--------------------------------------------------------------------
Python modules loaded from /home/xxx/python3.7/site-packages/PIL
Binary modules loaded from /home/xxx/python3.7/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.12
--- WEBP support ok, loaded 1.2.4
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for 9.0
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
........后面的输出结果省略........
from PIL import Image
im_path = './test.webp'
im = Image.open(im_path, 'r')
print(im)
成功!
这个问题在安装webp支持并且重装pillow之后也会得到解决。
原因在于,这些jpg图像文件本身是webp格式,在保存为图像的时候后缀写成了 .jpg 或者 .png 等,在windows环境或者一些特定的图像查看器,这样的图像文件可以正常打开。
但是pillow包和部分图像查看器无法正确识别该图像文件的格式,所以PIL无法成功读取文件。
因此,在安装webp支持后,该问题也得到解决。