【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形式的,再次加載,果然成功。

       總結:看樣子每次報錯並不一定都是代碼的問題,還有可能是圖片格式的問題,記錄於此,警醒自己……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章