解决linux环境python的PIL不支持处理webp格式图片的问题

问题描述

用pil库处理图片,读取png,用 .webp 保存,
本地调试正常
不过部署到linux上报错unknown file extension: .webp

File “/opt/anaconda/anaconda3/lib/python3.7/site-packages/PIL/Image.py”, line 2331, in save
format = EXTENSION[ext]
KeyError: ‘.webp’

im.save(out_img)
File “/opt/anaconda/anaconda3/lib/python3.7/site-packages/PIL/Image.py”, line 2333, in save
raise ValueError(f"unknown file extension: {ext}") from e
ValueError: unknown file extension: .webp

检查发现不支持webp格式

# python
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> import PIL
>>> PIL.__version__
'8.4.0'
>>> from PIL import features
>>> print(features.check_module('webp'))
False
>>> quit()

解决

1.安装相关依赖

sudo yum install libjpeg-devel libpng-devel libwebp-devel
$ sudo yum install libjpeg-devel libpng-devel libwebp-devel
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository base is listed more than once in the configuration
Package libjpeg-turbo-devel-1.2.90-8.el7.x86_64 already installed and latest version
Package 2:libpng-devel-1.5.13-7.el7_2.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package libwebp-devel.x86_64 0:0.3.0-10.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================================================
 Package                                                     Arch                                                 Version                                                       Repository                                             Size
============================================================================================================================================================================================================================================
Installing:
 libwebp-devel                                               x86_64                                               0.3.0-10.el7_9                                                updates                                                23 k

Transaction Summary
============================================================================================================================================================================================================================================
Install  1 Package

Total download size: 23 k
Installed size: 63 k
Is this ok [y/d/N]: y
Downloading packages:
libwebp-devel-0.3.0-10.el7_9.x86_64.rpm                                                                                                                                                                              |  23 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libwebp-devel-0.3.0-10.el7_9.x86_64                                                                                                                                                                                      1/1 
  Verifying  : libwebp-devel-0.3.0-10.el7_9.x86_64                                                                                                                                                                                      1/1 

Installed:
  libwebp-devel.x86_64 0:0.3.0-10.el7_9                                                                                                                                                                                                     

Complete!
(base) [tmpadm@szhwb2c3109 ~]$ 

如果有提示 No package libwebp-devel available., 是yum源的问题,在网上找方法解决就行

2.卸载pillow包

$ sudo /opt/anaconda/anaconda3/bin/pip uninstall pillow
Uninstalling Pillow-9.3.0:
  Would remove:
    /opt/anaconda/anaconda3/lib/python3.7/site-packages/PIL/*
    /opt/anaconda/anaconda3/lib/python3.7/site-packages/Pillow-9.3.0.dist-info/*
Proceed (y/n)? y
  Successfully uninstalled Pillow-9.3.0
$ 

3.重新安装pillow包

$ sudo /opt/anaconda/anaconda3/bin/pip --no-cache-dir install -I pillow==8.4.0  -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 
Looking in indexes: http://pypi.douban.com/simple/
Collecting pillow==8.4.0
  Downloading http://pypi.doubanio.com/packages/7d/2a/2fc11b54e2742db06297f7fa7f420a0e3069fdcf0e4b57dfec33f0b08622/Pillow-8.4.0.tar.gz (49.4MB)
     |████████████████████████████████| 49.4MB 10.9MB/s 
Building wheels for collected packages: pillow
  Building wheel for pillow (setup.py) ... done
  Stored in directory: /tmp/pip-ephem-wheel-cache-nvs9267m/wheels/20/47/fa/3058a5f2878b4df310d14de00834c6ffcc4d1809d26af80b26
Successfully built pillow
Installing collected packages: pillow
Successfully installed pillow-8.4.0

检查一下,问题解决,程序不报错了

$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> import PIL
>>> PIL.__version__
'8.4.0'
>>> from PIL import features
>>> print(features.check_module('webp'))
True

参考

https://www.javazxz.com/thread-9175-1-1.html
https://www.jb51.net/article/87235.htm
https://www.aiuai.cn/aifarm1903.html

你可能感兴趣的:(python,linux)