【Python】UserWarning: image file could not be identified because WEBP support not installed

       最近在研究使用Python进行图片裁剪的功能,发现Python的PIL模块在处理图片上有不可替代的优势,但是在使用open()方法加载图片时却报了如下的错误:

D:\anaconda\lib\site-packages\PIL\Image.py:2860: UserWarning: image file could not be identified because WEBP support not installed
  warnings.warn(message)
Traceback (most recent call last):
  File "D:/pycharmProjects/hdfs/接口调用/test.py", line 20, in <module>
    split_image("C://Users//ylxys//Desktop//image//640.jpg", 16)
  File "D:/pycharmProjects/hdfs/接口调用/test.py", line 11, in split_image
    img = Image.open(filename)
  File "D:\anaconda\lib\site-packages\PIL\Image.py", line 2862, in open
    "cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file '640.jpg'

       小编的测试代码是这样的:

from PIL import Image

img = Image.open("C://Users//ylxys//Desktop//image//640.jpg")
print(img)

       很简单的测试代码但是就是在调用open()方法的时候不知道除了什么问题。搜索资料无果的情况下,我把注意力放到了这样的一条警告上:

D:\anaconda\lib\site-packages\PIL\Image.py:2860: UserWarning: image file could not be identified because WEBP support not installed
  warnings.warn(message)

       因为我对WEBP这几个字母比较敏感,图片是我从网络上保存下来的,格式是WEBP的,为了更加的亲民,我采用了一种掩耳盗铃的方式,直接将图片后缀改成了jpg。考虑到这一点,我立即使用上面的代码加载了本地的另外一张jpg图片,果然顺利通过测试。那么接下来就是将后缀为WEBP的图片改成格式为jpg的图片了。

       首先我们需要将格式为webp的图片在谷歌浏览器中打开,方便起见,可以直接打开谷歌浏览器将图片拖进去即可。然后右键复制图片,将图片发送到QQ窗口,在QQ中保存成jpg形式的,再次加载,果然成功。

       总结:看样子每次报错并不一定都是代码的问题,还有可能是图片格式的问题,记录于此,警醒自己……

猜你喜欢

转载自blog.csdn.net/gdkyxy2013/article/details/106257173