0

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?

mrgou
  • 1,576
  • 2
  • 21
  • 45
  • 1
    You checked out [this question](https://stackoverflow.com/questions/55349110/webp-support-not-installed-error-with-pillow-included-in-anaconda)? – David Wolf Feb 06 '22 at 21:06
  • I remember bumping into it a while ago when I first encountered this issue (the question is nearly 3 year old). Now, the error I get is slightly different than the one I did back then. I've tried tracing what triggers the `UserWarning` in the PIL code to figure out what it's missing, but it's way beyond my league. – mrgou Feb 06 '22 at 21:42

2 Answers2

0

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
mrgou
  • 1,576
  • 2
  • 21
  • 45
0

The question is whether Anaconda actually builds Pillow with webp support. Looking on the anaconda website I couldn't determine this.

However, conda-forge does build Pillow with webp support, see here. So you might want to consider using that.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • I know, and that's the package that doesn't work on Windows. conda-forge is always my highest priority channel. – mrgou Feb 12 '22 at 20:02