python PIL open cannot open webp, jpeg and other images, and reports the error PIL.UnidentifiedImageError: cannot identify image file

Problem: Most image files can be opened normally using PIL.Image.open(), but images in webp format cannot be opened. Some jpg and png images cannot be opened, and an error is reported:

PIL.UnidentifiedImageError: cannot identify image file xxx

The test found that these images can be viewed normally using a picture viewer in a Windows environment, indicating that the problem is not with the image file itself.

from PIL import Image


# im_path = './test.webp'
im_path = './test.jpg'
im = Image.open(im_path, 'r')
print(im)

At the same time, the above code can open other normal images, indicating that there is no problem with the code.

1. Unable to open webp file

1. Check whether the machine has webp support

It may be that there is a problem with the pillow package that prevents the image from being opened. When installing the pillow package, there is no webp support, resulting in the inability to read the webp file.

You can check whether the pillow installed on this machine has webp support through the following method:

from PIL import features

print(features.pilinfo())  # info of pillow
# print(features.check_module('webp'))  # if webp support, True or False

As can be seen from the output of the following code (red font part), the pillow installed locally does not have webp support :

--------------------------------------------------------------------
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
-------------------------------------------------- ------------------......................
Omitted here..... ....................................
--------- -------------------------------------------------- ---------
XPM image/xpm
Extensions: .xpm
Features: open
---------------------------- -------------------------------------
XVTHUMB
Features: open
-------- -------------------------------------------------- ----------
None

2. Install webp support and reinstall the pillow package

First, you need to install webp support for this machine, and then reinstall the pillow package.

For Unix-based computers, enter the following command to install webp support:

sudo apt-get install libwebp-dev

For maxos, the command is as follows:

brew install webp

Then you need to reinstall the pillow package in anaconda. To be safe, it is recommended that readers create a new anaconda virtual environment, and then uninstall and reinstall the package in the newly created environment to avoid problems during the reinstallation process that cause errors in the existing environment, and there will be no place to cry. . . . . .

conda uninstall pillow
conda install pillow

Then I tested and found that pillow already has webp support.

--------------------------------------------------------------------
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
--------------------------------------------------------------------
........后面的输出结果省略........

3. Test reading webp file

from PIL import Image


im_path = './test.webp'
im = Image.open(im_path, 'r')
print(im)

success!

2. Reading some jpg files also reports the error PIL.UnidentifiedImageError: cannot identify image file xxx

This problem will also be solved after installing webp support and reinstalling pillow.

The reason is that these jpg image files themselves are in webp format, and when saved as images, the suffix is ​​written as .jpg or .png, etc. In the Windows environment or some specific image viewers, such image files can be opened normally.

However, the pillow package and some image viewers cannot correctly recognize the format of the image file, so PIL cannot successfully read the file.

So after installing webp support the issue was also resolved.

Guess you like

Origin blog.csdn.net/me_yundou/article/details/132299965