解决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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值