Error while converting webp image file to jpg in python
Asked Answered
K

5

16

I have written small program to convert webp to jpg in python

import imghdr
from PIL import Image

im = Image.open("unnamed.webp").convert("RGB")
im.save("test.jpg","jpeg")

when executing it gives me following error

No handlers could be found for logger "PIL.ImageFile"
Traceback (most recent call last):
  File "webptopng.py", line 3, in <module>
    im = Image.open("unnamed.webp").convert("RGB")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2286, in open
    % (filename if filename else fp))
IOError: cannot identify image file 'unnamed.webp'

I have installed pillow with webp capability. Here is my pillow installation output

--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version      Pillow 3.0.0
platform     linux2 2.7.3 (default, Jun 22 2015, 19:33:41)
             [GCC 4.6.3]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
*** OPENJPEG (JPEG2000) support not available
--- ZLIB (PNG/ZIP) support available
*** LIBTIFF support not available
--- FREETYPE2 support available
*** LITTLECMS2 support not available
--- WEBP support available
*** WEBPMUX support not available
--------------------------------------------------------------------

Please help me how to proceed.

Kutch answered 23/10, 2015 at 6:40 Comment(4)
I've never used WebP, but your code should work. Can you verify that "unnamed.webp" is actually a valid WebP file, eg using ImageMagick's identify or convert commands?Kiblah
Here is output of file command in linux $ unnamed.webp: RIFF (little-endian) data output of imagemagick $ convert: no decode delegate for this image format 'unnamed.webp' @ error/constitute.c/ReadImage/532. convert: missing an image filename '/dev/null' @ error/convert.c/ConvertImageCommand/3011.Kutch
Sorry, I should've mentioned that older versions of ImageMagick may not have WebP surpport. All I can suggest is to do a hexdump to check that the first 12 bytes of the file match the header shown in the WebP Wikipedia article. And maybe try to find some more WebP files to test.Kiblah
File is correct I have verified from wikipediaKutch
K
8

This issue has been resolve now. I have install latest libwebp library i,e libwebp-0.4.3 and reinstall pillow.

Here is github issue thread if some one face same issue.

Kutch answered 26/10, 2015 at 4:22 Comment(0)
R
14

I tested your code with a webp image and it works with Pillow 2.9:

$ wget https://www.gstatic.com/webp/gallery3/2_webp_a.webp
>>> from PIL import Image
>>> im = Image.open("2_webp_a.webp").convert("RGB")
>>> im.save("test.jpg","jpeg")

There's Pillow 3.0 issue #1474 related with your error.

Let's you try to downgrade Pillow from 3.0 to 2.9 and try again.

Rachitis answered 23/10, 2015 at 8:32 Comment(4)
Still not working with Pillow 2.9.0 getting the same error $ pip freeze | grep -i Pillow Pillow==2.9.0Kutch
Did you tried with the same webp image and the same code I used ? with the same Pillow version ? Than I suggest you to open an issues on Pillow's github pageRachitis
Yes I have used same code and same file as mentioned by youKutch
@SandeepKumarSingh I saw your issue on Pillow's github page, is the problem that you have an old webp library ?Rachitis
K
8

This issue has been resolve now. I have install latest libwebp library i,e libwebp-0.4.3 and reinstall pillow.

Here is github issue thread if some one face same issue.

Kutch answered 26/10, 2015 at 4:22 Comment(0)
E
2

Install webptools by pip install webptools and then:

from webptools import dwebp
print(dwebp("python_logo.webp","python_logo.jpg","-o"))

This library works bit slow but is easy to do your work.

Etam answered 22/7, 2020 at 8:31 Comment(0)
R
1

Load image with webp and proceed

from PIL import Image
import webp

im = webp.load_image('test.webp').convert('RGB')
im.save('test.jpg', 'jepg')
Rory answered 23/9, 2022 at 8:16 Comment(0)
V
-1
from PIL import Image

src = 'box.webp'
dst = 'box.jpg'
bgColor = 'white'

# Convert to RGB with Alpha channel
im = Image.open(src).convert('RGBA')

# Get Alpha channel mask
mask = im.split()[3]

# Create new RGB image with background color (transparent to bgColor)
rgb = Image.new('RGB', im.size, bgColor)

# Paste original image with mask
result = Image.composite(im, rgb, mask)

# Save result to JPG
result.save(dst, 'JPEG')

# Bad example. Save with artefacts
Image.open(src).convert('RGB').save('bad_' + dst, 'JPEG')

https://i.sstatic.net/VE3F3.png

Ventriculus answered 22/12, 2023 at 14:41 Comment(2)
Get Error message with your suggested proposal.Nolie
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Binal

© 2022 - 2024 — McMap. All rights reserved.