0

In python code, I need to process webp images. But when I try to open it with python PIL module, I have an error: OSError: cannot identify image file 'my_image.webp

My Deep Learning image is created from GCP Marketplace VM (tensorflow image), but it seems that webp format is not "activated" at the pillow level.

Is the webp format supported in python by default? What do I need to do/install/import on the VM to be able to open webp images with python PIL?

My python code steps:

>>>import PIL
​
>>>print(PIL.__version__)
6.0.0.post0

>>>from PIL import features
>>>print (features.check_module('webp'))
False

>>> PIL.Image.open('my_image.webp')
/usr/local/lib/python3.5/dist-packages/PIL/Image.py:2703: UserWarning: image file could not be identified because WEBP support not installed
  warnings.warn(message)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-99a62d35da67> in <module>
----> 1 PIL.Image.open('BATIMENT0000000045936174_flatRoof.webp')

/usr/local/lib/python3.5/dist-packages/PIL/Image.py in open(fp, mode)
   2703         warnings.warn(message)
   2704     raise IOError("cannot identify image file %r"
-> 2705                   % (filename if filename else fp))
   2706 
   2707 #

OSError: cannot identify image file 'my_image.webp'
4

2 回答 2

1

Open JupyterLab UI of your GCP VM and run a Terminal session. In the terminal run these commands to install webp library:

pip uninstall Pillow
pip uninstall Pillow-SIMD
sudo apt install libwebp-dev
pip install Pillow-SIMD

Restart your Jupyter kernel. Now PIL is able te read webp images.

于 2019-11-07T15:09:56.700 回答
1

Ran into a similar issue on one of my servers.

Used the commands mentioned above, but was still getting False when running features.check_module('webp')

Turns out when reinstalling Pillow-SIMD, you need to make sure you're not using the cached version of the build, otherwise you won't get the WEBP support. So changing the last step to: pip install Pillow-SIMD --no-cache-dir solved it for me.

I would've added this as a comment but I don't have enough rep!

于 2020-02-04T17:17:44.870 回答