On an Anaconda set up, I want to run following simple code:
from PIL import Image
img = Image.open('test.webp')
This works on my Linux machine, but on Windows:
UserWarning: image file could not be identified because WEBP support not installed
warnings.warn(message)
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-3-79ee787a81b3> in <module>
----> 1 img = Image.open('test.webp')
~\miniconda3\lib\site-packages\PIL\Image.py in open(fp, mode, formats)
3028 for message in accept_warnings:
3029 warnings.warn(message)
-> 3030 raise UnidentifiedImageError(
3031 "cannot identify image file %r" % (filename if filename else fp)
3032 )
UnidentifiedImageError: cannot identify image file 'test.webp'
I do have the libwebp
package installed, and libwebp.dll
is present in the Library\bin
directory of my Anaconda set up.
Any idea?
Looks like no solution is in sight. Assuming dwebp
from the libwepb
library is available in the system, this works for me to generate a Pillow Image
object from a WEBP file:
import os
import subprocess
from PIL import Image, UnidentifiedImageError
from io import BytesIO
class dwebpException(Exception):
pass
def dwebp(file: str):
webp = subprocess.run(
f"dwebp {file} -quiet -o -", shell=True, capture_output=True
)
if webp.returncode != 0:
raise dwebpException(webp.stderr.decode())
else:
return Image.open(BytesIO(webp.stdout))
filename = 'test.webp'
try:
img = Image.open(filename)
except UnidentifiedImageError:
if os.path.splitext(filename)[1].lower() == ".webp":
img = dwebp(filename)
else:
raise